//数组对象去重
var temp = {};
this.tableData = this.tableData.reduce((prev, curv) => {
//this.tableData是数组
// 如果临时对象中有这个名字,什么都不做
if (temp[curv.sku_good_code]||!curv.sku_good_code) {
}//curv.sku_good_code数组对象里字段
//!curv.sku_good_code 去除空对象
// 如果临时对象没有就把这个名字加进去,同时把当前的这个对象加入到prev中
else {
temp[curv.sku_good_code] = true;
prev.push(curv);
}
return prev;
}, []);
//findIndex配合splice删除数组中指定id的数据this.good_data:[{goods_id:1,name:王芳}{goods_id:2,name:王芳}{goods_id:3,name:王芳}] this.goods_row:[1,2]
this.goods_row.forEach((current) => {
let ind = this.good_data.findIndex(e=>e.goods_id == current);
this.good_data.splice(ind,1)
});
//返回[{goods_id:3,name:王芳}]
stu.find((element) => (element.name == '李四')); //返回的是{name: "李四", gender: "男", age: 20}这个元素
stu.findIndex((element)=>(element.name =='李四')); //返回的是索引下标:2
//map处理数组
var datass = [{
url: 1,
img: 2,
name: 3,
code: 4
}]
datass = datass.map(item => ({
url: 9,
img: 8
})
);
console.log(datass)// [{url: 9, img: 8}]
数组对象去重
unrepeated(data, param) { // data 数组 param表示去重的唯一值
var temp = {};
var repeatLength = []
var tempData = JSON.parse(JSON.stringify(data))// 深拷贝原来的数据
var unrepeated = tempData.reduce((prev, curv) => {
if (!(temp[curv[param]] || !curv[param])) {
temp[curv[param]] = true;
repeatLength[curv[0]] = 1
prev.push(curv);
} else {
repeatLength[curv[0]]++
}
return prev;
}, []);
return { unrepeated: unrepeated, repeatLength: repeatLength };
},