3 回答
TA贡献1796条经验 获得超10个赞
let data = [{
ym: '1',
id: 1
}, {
ym: '2',
id: 2
}, {
ym: '1',
id: 3
}]
function trans (arr) {
let cache = {}
return data.reduce((sum, cur) => {
let index = cache[cur.ym]
if (index !== undefined) {
sum[index].push(cur)
} else {
sum.push([cur])
cache[cur.ym] = sum.length - 1
}
return sum
}, [])
}
trans(data)
TA贡献1725条经验 获得超7个赞
var keyArr= [];
var jsonArr = [];
for(let i in jsonStr){
let str = jsonStr[i];
if(keyArr.include(str.ym)){
let index = keyArr.indexOf(str.ym);
jsonArr.push(str)
}else{
keyArr.push(str.ym);
let index = keyArr.length-1;
jsonArr.push(str)
}
}
return jsonArr;
TA贡献1834条经验 获得超8个赞
const data = [
{ym: 123},
{ym: 345},
{ym: 123},
{ym: 345},
]
let result = []
let subArr = [] // 用于去重(避免同一组ym出现在多个数组中)
data.forEach(item1 => {
const ym = item1.ym
if(!subArr.includes(ym)){
subArr.push(ym)
result.push(data.filter(item2 => ym === item2.ym))
}
})
添加回答
举报