Addition and deletion of WeChat applet array objects (Vue2)

1. Add

Two methods of adding elements to an array (neither deduplication)

1. Array.push(object)

Append new elements directly to the end of the array (without deduplication)

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

insert image description here

When using push to add an array, the entire array will be directly added to the array

insert image description here

1. Array.concat(object)

Using concat will split the data (array) to be appended to append to the end of the data

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

Append the same data
insert image description here

Append a group of arrays
![Insert picture description here](https://img-blog.csdnimg.cn/2416833d89a5466db0a2cc9db83bf2df.png
3. Array deduplication

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

Two, delete

1. Use splice to delete elements

Delete the elements in the array (delete by finding the subscript of the object); index ==-1 means the array is empty

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

Guess you like

Origin blog.csdn.net/qq_44774287/article/details/127508777