⼩程序js数组初始化_⼩程序之⼀:JS操作.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)
获取系统信息 下⾯是屏幕宽⾼
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 Time() + 24 * 60 * 60 * 1000) var date_value1 = FullYear().toString()
var date_value2 = (Month() + 1).toString()
var date_value3 = Date().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.place(/-/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 = String();
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 = String();
if (minStr.length == 1) minStr = '0' + minStr;
// 秒位
var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
var secStr = String();
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);
},

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。