数组push方法:
可以把请求到的数据累积放在数组中
this.list.push(...res.data.data.results)
数组从前往后添加方法unshift:
var arr = ["张三", "李四", "王五"];
var count = arr.unshift("赵六", "小明");
console.log(count);
console.log(arr);
此时arr=[ “赵六”, “小明” , “张三”, “李四”, “王五” ]
数组删除元素splice方法:
array.splice(index,length)意思是删除的时候,从哪里开始删除,删除多少,是几个;
goods_cart.splice(index,1);
数组的every方法:
every方法会遍历,会接受一个回掉函数,如果每一个回掉函数返回的都是true,那么every方法返回的也是true;
只要有一个回掉函数返回false,那么循环不在执行,直接返回false
如果是空数组,你也调用了该方法,那么的返回值就是true
如何解决,通过三元表达式判断一下数组的长度即可
const allchecked=goods_cart.length?every(v=>v.checked=true):false;
筛选数组map方法:
1.map是针对数组的方法pics.map(v=>v.pics_mid),pics是一个元素为对象的数组,map方法将数组的元素一个个拿出来,也就是我们这里的v,v.pics_mid是将每个对象中的pics_mid挑选出来,然后组成一个新的数组
handlePreivewImage(e){
// 1先构造要预览的图片数组
const urls=this.GoodsInfo.pics.map(v=>v.pics_mid);
const current=e.currentTarget.dataset.url;
console.log(e);
wx.previewImage({
// current接受传递过来的参数
current,
urls
})
}
查询数组满足条件的findIndex方法:
findIndex()函数也是查找目标元素(查找符合条件的第一个元素),找到就返回元素的位置,找不到就返回-1。
let index=cart.findIndex(v=>v.goods_id===this.GoodsInfo.goods_id);
代码解释:cart是一个数组,findIndex将数组中的每一个元素拿出来,v.goods_id===this.GoodsInfo.goods_id这个是判断条件,如果有满足条件的元素,它的结果是索引值;如果没有满足条件的,它的返回值是-1
数组查重的indexof方法:
如果数组中又满足条件的元素,就会返回第一个满足条件的元素的索引值;如果没有的话就会返回-1
const index = this.historyList.indexOf(suggestion)
if (index === -1) {
this.historyList.unshift(suggestion)
}
数组的结合写法:
goodsList:[...this.data.goodsList,...result.data.message.goods]
数组清空:
让数组等于[ ]就可以了
比如goodsList是一个数组,现在想要清空它,可以goodsList=[ ]
两个数组对象的去重:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j8uIaV9u-1617330645358)(D:\微信小程序笔记\数组\453f10abdfcafc0cc7cd949bd0e7c5e.png)]
totalChannel:
myChannel:
recommendChannel:
recommendChannel = totalChannel - myChannel
(1)使用ES6的快捷方法:
注意这几个return的功能
recommendChannel: function () {
return this.totalChannel.filter((value1, j) => {
return !this.mychannel.find(value2 => {
return value1.id === value2.id
})
})
}
(2)通过循环
这样循环的时候使用标识符,根据标识符的true或者false决定要进行什么样的操作
recommendChannel2: function () {
const arr1 = []
this.totalChannel.forEach((value) => {
let flag = false
for (let i = 0; i < this.mychannel.length; i++) {
if (this.mychannel[i].id === value.id) {
flag = true
}
}
if (!flag) {
arr1.push(value)
}
})
return arr1
}