.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ù)。