微信小程序之一:JS操作

.map 方法示例 接口返回的imageURL拼接根地址
var tempArray = '接口返回的數(shù)組'
that.setData({
    listArray: tempArray.map(function(item) {
        item.imageURL = getApp().data.baseURL + item.imageURL;
        //如果列表里沒有子數(shù)組數(shù)據(jù)需要拼接根地址,就不用再次 .map 方法 直接return item
        item.child.map(function(childItem) {
          childItem.pic = getApp().data.baseURL + childItem.pic;
          return childItem;
        })
        return item;
    }),
})
.filter方法 遍歷數(shù)組數(shù)據(jù)
示例:遍歷cityArray 數(shù)組,找出cityArray數(shù)組中pid 等于
province 數(shù)組中province[e.detail.value].id的值
使用.filter方法 比 for 循環(huán)速度快

let newArray = that.data.cityArray.filter(item => item.pid === that.data.province[e.detail.value].id)
獲取系統(tǒng)信息 下面是屏幕寬高
 wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
        })
      }
});
動態(tài)設(shè)置頁面標題
第一種:定死頁面標題修改 .json文件
{
  "navigationBarTitleText": "標題"
}

第二種:動態(tài)自定義頁面標題
wx.setNavigationBarTitle({
  title: this.data.URLBackTitle
})
本地數(shù)據(jù)存儲
 // 同步方式存儲數(shù)據(jù)
wx.setStorageSync('userInfo', this.data.userInfo);

//取出數(shù)據(jù)
var token = wx.getStorageSync('userInfo')

頁面棧層級數(shù)
var pages = getCurrentPages();
console.log('頁面棧層數(shù)' + pages.length)
if (pages.length == 10) {
//小程序最大10層
}else{
}
獲取明天日期
//獲取當前時間
var nowDate = new Date(new Date().toLocaleDateString()); 
//獲取明天這個時間
var nowDate1 = new Date(nowDate.getTime() + 24 * 60 * 60 * 1000)

    var date_value1 = nowDate1.getFullYear().toString()
    var date_value2 = (nowDate1.getMonth() + 1).toString()
    var date_value3 = nowDate1.getDate().toString()

    if (date_value2.length == 1) {
      date_value2 = '0' + date_value2
    }
    if (date_value3.length == 1) {
      date_value3 = '0' + date_value3
    }
    
    var tomorrow = date_value1 + '-' + date_value2 + '-' + date_value3
倒計時
LastTimer: function() {

var that = this;
var totalSecond = 0;
//start_time服務(wù)端返回的團購開始時間
var start_time = new Date(that.data.start_time.replace(/-/g, "/"));
totalSecond = Date.parse(start_time) / 1000 - Date.parse(new Date()) / 1000;

    //清理掉倒計時
    clearInterval(that.data.interval_daojishi);
    //開始倒計時
    that.data.interval_daojishi = setInterval(function() {
      // 秒數(shù)
      var second = totalSecond;
      // 天數(shù)位
      var day = Math.floor(second / 3600 / 24);
      var dayStr = day.toString();
      if (dayStr.length == 1) dayStr = '0' + dayStr;
      // 小時位
      var hr = Math.floor((second - day * 3600 * 24) / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;
      // 分鐘位
      var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;
      // 秒位
      var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;

      that.setData({
        countDownDay: dayStr,
        countDownHour: hrStr,
        countDownMinute: minStr,
        countDownSecond: secStr,
      });
      totalSecond--;

      if (totalSecond < 0) {
        //倒計時結(jié)束,清理掉倒計時
        clearInterval(that.data.interval_daojishi);
        that.setData({
          countDownDay: '00',
          countDownHour: '00',
          countDownMinute: '00',
          countDownSecond: '00',
        });
      }
    }.bind(this), 1000);
  },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,621評論 1 32
  • 最近在做一款醫(yī)療健康項目時,在想一個問題:目前的產(chǎn)品流程、功能、交互是否達到了我們的預(yù)期,或者說用戶在使用中會不會...
    崔念閱讀 645評論 0 0
  • 新年第一天的驚喜,邂逅一場精彩的音樂?。旱抡Z經(jīng)典音樂劇《莫扎特》。 說真的,本來并未對這個音樂劇抱有太多期待,一是...
    午后的輕語呢喃閱讀 600評論 0 1
  • 小明大學(xué)畢業(yè)后的第一年攢了一萬元,雖然不多,但是有攢錢的意識并且能攢下錢,對于一個剛畢業(yè)進入社會的新人并不容易。 ...
    果冪的N次方閱讀 281評論 0 1
  • 感恩宇宙的厚愛,我總是幸運的,一切美好的事情總是發(fā)生在我身上。 感謝以最好的價格買到了我最愛的家具。 謝謝磊磊寄來...
    李小娣ILoveu閱讀 121評論 0 0

友情鏈接更多精彩內(nèi)容