JS——把数组中拥有相同值的部分放在一起
2020-12-10 本文已影响0人
Hasan_hs
tableData2: [
{
name: "项目1",
birth: "80m²",
status: "已确认",
time: "2020-01-01",
zhuanye: "金水区资源局",
isOption: false,
},
{
name: "项目2",
birth: "80m²",
status: "已确认",
time: "2020-01-01",
zhuanye: "金水区资源局",
isOption: false,
},
{
name: "项目3",
birth: "80m²",
status: "待确认",
time: "-- --",
zhuanye: "金水区税务局",
isOption: true,
},
{
name: "项目4",
birth: "80m²",
status: "已确认",
time: "-- --",
zhuanye: "金水区资源局",
isOption: true,
},
],
js部分
checkSameData(tableData2){
let cache = {}; //存储的是键是zhuanye 的值,值是zhuanye 在indeces中数组的下标
let indices = []; //数组中每一个值是一个数组,数组中的每一个元素是原数组中相同zhuanye的下标
tableData2.map((item,index)=>{
let zhuanye = item.zhuanye;
let _index = cache[zhuanye];
if(_index!==undefined){
indices[_index].push(index)
}else{
cache[zhuanye] = indices.length
indices.push([index])
}
})
let result = [];
indices.map((item)=>{
item.map((index)=>{
result.push(tableData2[index])
})
})
this.tableData2 = result
},