数组内对象去重 #147
Anuluca Date : 2024-08-14 Tags : 2
1.字符串数组去重
1 2 3
| const arr = ['皮卡丘','野狗','皮卡丘'] const set = Array.from(new Set(arr)); console.log(set);
|
2.对象数组去重
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| const pokemons = [ {id: 001, name: "妙蛙种子"}, {id: 002, name: "妙蛙草"}, {id: 003, name: "妙蛙花"}, {id: 000, name: "妙蛙种子"}, ];
let obj = {};
let pokemons_after = pokemons.reduce((cur,next) => { obj[next.name] ? "" : obj[next.name] = true && cur.push(next); return cur; },[]) console.log(pokemons_after);
|