【前端芝士树】如何完成数组的去重 Array Unique?
【前端芝士树】如何完成数组的去重 Array Unique?
输入
const users = [{ id: 1, name: "a" }, { id: 2, name: "a" }, { id: 3, name: "b" }, { id: 4, name: "v" }];
推荐方法
Array.prototype.unique = function () { let arr = this.map((item) => {return item.name }); return Array.from(new Set(arr));};
其他方法
1. 利用obj实现数据的去重
Array.prototype.unique2 = function () { let obj = {}; let arr = []; this.forEach((item) => { if(!obj[item.name]){ obj[item.name] = true; } }); for(let key in obj){ arr.push(key); } return arr; };
相关推荐
liduote 2020-11-13
chenhaotao 2020-11-13
localhost0 2020-11-12
小秋 2020-11-12
lxhuang 2020-11-03
学习web前端 2020-10-27
小焊猪web前端 2020-10-24
杏仁技术站 2020-10-23
南昌千网科技 2020-10-18
liduote 2020-10-16
BlueSkyUSC 2020-10-15
Doniet 2020-10-08
zjutzmh 2020-09-25
PncLogon 2020-09-24
趣IT 2020-09-22
杏仁技术站 2020-09-18
拾光璇旅 2020-09-17
kiven 2020-09-11
lfbooo 2020-09-09