js实现数组去重

 js实现数组去重_第1张图片

//js对象去重
let arr = [
    { id: 1, name: '2' },
    { id: 1, name: '2' },
    { id: 3, name: '3' },
];
let uniqueArr = Array.from(new Set(arr.map(JSON.stringify)), JSON.parse);
console.log(arr.map(JSON.stringify),'arr.map(JSON.stringify)');
console.log(new Set(arr.map(JSON.stringify)),'new Set(arr.map(JSON.stringify))');
console.log(uniqueArr,'uniqueArr');

//文本去重
const names = ["John", "Jane", "John", "Alice", "Bob", "Jane"];
const uniqueNames = Array.from(new Set(names));
console.log(uniqueNames,'uniqueNames');

你可能感兴趣的:(数据结构处理,javascript)