例如你需要这样格式的数据:
[{"name":"合肥市","arrey":[{"lat":"31.862323","long":"117.289267","name":"G+竞技网咖","address":"宿州路60号太阳城B1层"},{"lat":"31.876318","long":"117.349632","name":"H&M(瑶海万达广场店)","address":"临泉东路与和县路交叉口瑶海万达广场1层"},{"lat":"31.790946","long":"117.207581","name":"N(港澳广场)","address":"翡翠路与芙蓉路交口往北50米处港澳广场F1层"},{"lat":"31.883742","long":"117.280290","name":"U家鱼店","address":"蒙城路与义井路交叉口"},{"lat":"31.863728","long":"117.289816","name":"L‘OREAL(百大鼓楼名品中心店)","address":"宿州路96号百大鼓楼名品中心金座1层"},{"lat":"31.865195","long":"117.292521","name":"T.G","address":"宿州路老太阳城三楼55号"},{"lat":"31.864289","long":"117.294944","name":"e(合肥百盛)","address":"淮河中路77号合肥百盛F1层"},{"lat":"31.864579","long":"117.293090","name":"W.(海亚大世界)","address":"北含山路与淮河路步行街交叉口东北100米海亚大世界F3层N13"},{"lat":"31.610835","long":"117.883709","name":"s·deer","address":"东塘路巢湖万达广场1层"},{"lat":"31.704786","long":"117.301498","name":"宝文MALL","address":"南宁路与庐州大道交叉口西南150米"},{"lat":"31.804920","long":"117.223823","name":"置地广场D座","address":"天鹅湖南岸(绿轴西侧)"}]},{"name":"无为县","arrey":[{"lat":"31.290454","long":"117.931186","name":"东方家园B区","address":"军二路与渡江路交汇处"},{"lat":"31.450142","long":"117.895212","name":"横山","address":"无为县"}]},{"name":"安庆市","arrey":[{"lat":"30.530881","long":"117.069820","name":"文采花园F座","address":"旺园路南100米"}]}]
先从缓存中获取数据,判断当前数据是否在你获取的数据里面没有的话就添加进去,有的话就不添加,超过10个的话就删除
实现:
var obj = {
name: ‘合肥’,
arrey: [{
lat: ‘31.82057’,
long: ‘117.227308’,
name: ‘合肥汽车站’,
address: ‘合肥汽车站餐馆’
}]
}//点击的当前 wx.getStorage({
key: "bigKSarr",
success(res) {
console.log(res.data) var bigKSarr = [];//命名大的数组 bigKSarr = res.data; //判断有没有当前城市
let nameInside = bigKSarr.some(function (e) {
return JSON.stringify(e.name) === JSON.stringify(obj.name);
})
if (!nameInside) {
//没有就添加进去
bigKSarr.push(obj)
} bigKSarr.forEach(function (el, i) { if (el.name == obj.name) {
//同一个城市
let inside = el.arrey.some(function (item) {
return JSON.stringify(item) === JSON.stringify(obj.arrey[0]);
})
if (!inside) {
////判断是否包含在某个城市的下列里面没有则push进去有的话就不push
el.arrey.push(obj.arrey[0]);
}
}
//如果数组超过10个就删除第一个
if (el.arrey.length > 10) {
el.arrey.splice(0,1)
}
})
wx.setStorage({
key: "bigKSarr",
data: bigKSarr
}) },
fail(res) {
//第一次获取缓存失败
console.log(res) var bigKSarr = [];//命名大的数组
var ksli = {
lat: e.currentTarget.dataset.latlog.split(",")[1],
long: e.currentTarget.dataset.latlog.split(",")[0],
name: e.currentTarget.dataset.name,
address: e.currentTarget.dataset.address
}//命名城市对应数组得单个对象
var ksliArr = [];
ksliArr.push(ksli)
var ksli = {
"name": appInstance.globalData.defaultCounty,
"arrey": ksliArr
}//这个是名字对应数组得li
bigKSarr.push(ksli)
wx.setStorage({
key: "bigKSarr",
data: bigKSarr
})
} })
这个功能是在小程序缓存所需要的,所以上述代码用了,小程序获取小程序缓存的方法