.map 方法示例 接口返回的imageURL拼接根地址
var tempArray = '接口返回的数组'
that.setData({
listArray: tempArray.map(function(item) {
item.imageURL = getApp().data.baseURL + item.imageURL;
//如果列表里没有子数组数据需要拼接根地址,就不用再次 .map 方法 直接return item
item.child.map(function(childItem) {
childItem.pic = getApp().data.baseURL + childItem.pic;
return childItem;
})
return item;
}),
})
.filter方法 遍历数组数据
示例:遍历cityArray 数组,找出cityArray数组中pid 等于
province 数组中province[e.detail.value].id的值
使用.filter方法 比 for 循环速度快
let newArray = that.data.cityArray.filter(item => item.pid === that.data.province[e.detail.value].id)
获取系统信息 下面是屏幕宽高
wx.getSystemInfo({
success: function(res) {
that.setData({
windowHeight: res.windowHeight,
windowWidth: res.windowWidth,
})
}
});
动态设置页面标题
第一种:定死页面标题修改 .json文件
{
"navigationBarTitleText": "标题"
}
第二种:动态自定义页面标题
wx.setNavigationBarTitle({
title: this.data.URLBackTitle
})
本地数据存储
// 同步方式存储数据
wx.setStorageSync('userInfo', this.data.userInfo);
//取出数据
var token = wx.getStorageSync('userInfo')
页面栈层级数
var pages = getCurrentPages();
console.log('页面栈层数' + 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服务端返回的团购开始时间
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() {
// 秒数
var second = totalSecond;
// 天数位
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) {
//倒计时结束,清理掉倒计时
clearInterval(that.data.interval_daojishi);
that.setData({
countDownDay: '00',
countDownHour: '00',
countDownMinute: '00',
countDownSecond: '00',
});
}
}.bind(this), 1000);
},