Last active: 3 years ago
数组去重合并
let arr = [
{ name: 'zs', age: 15, happy: ['唱歌', '玩游戏'] },
{ name: 'ls', age: 15, happy: ['玩游戏'] },
{ name: 'zs', age: 15, happy: ['学习', '划水'] },
];
function test(arr) {
let _obj = {};
let _arr = [];
if (!arr.length) return;
arr.map((item) => {
_obj[item.name]?.happy.length
? (_obj[item.name].happy = [..._obj[item.name].happy, ...item.happy])
: (_obj[item.name] = item);
});
_arr = Object.values(_obj);
return _arr;
}
let arr2 = test(arr);
console.log(arr2);
let arr = [
{ name: 'zs', age: 15, happy: ['唱歌', '玩游戏'] },
{ name: 'ls', age: 15, happy: ['玩游戏'] },
{ name: 'zs', age: 15, happy: ['学习', '划水'] },
];
function test(arr) {
let _obj = {};
let _arr = [];
if (arr.length)
arr.map((item) => {
_obj[item.name]?.happy.length
? (_obj[item.name].happy = _obj[item.name].happy.concat(item.happy))
: (_obj[item.name] = item);
});
_arr = Object.values(_obj);
return _arr;
}
let arr2 = test(arr);
console.log(arr2);