微信小程序数组对象的添加及删除(Vue2)

一、添加

数组添加元素的两个方法(都不去重)

1、数组.push(对象)

直接向数组末尾追加新的元素(不会去重)

//this.productTemporary=[]
this.productTemporary.push(e);

在这里插入图片描述

使用push添加数组时会将整个数组直接加入数组之中

在这里插入图片描述

1、数组.concat(对象)

使用concat会将要追加的数据(数组)进行拆分以此追加到数据末尾

//this.products=[]
this.products = this.products.concat(res.result.items); //追加新数据

追加相同的数据
在这里插入图片描述

追加一组数组
![在这里插入图片描述](https://img-blog.csdnimg.cn/2416833d89a5466db0a2cc9db83bf2df.png
3.数组去重

//arr:需要被去重的数组
res = new Map();
v = arr.filter(arr => !res.has(arr.productId) && res.set(arr.productId, arr.productId));

二、删除

1.使用splice删除元素

删除数组中的元素(通过找出该对象的下标来删除);index ==-1表示数组为空

// this.productTemporary数组
//item需要删除的元素
const index = this.productTemporary.findIndex(i => i == item)
						if (index != -1) {
							this.productTemporary.splice(index, 1)
						}