我们知道 一般说起去重 我们都会用到现在es6的Set集合,或者配个Array.from方法使用
像这样 const arr = [1,2,3,5,5,7,7,5,4]
const newArr = Array.from(new Set(arr))
console.log(newArr) // [1,2,3,5,7,4]
像这样简单的数组去重可以这样用 但是如果是二维数组或者多维数组 或者是数组里面是对象形式的引用类型的该怎么去重呢 接下来就介绍一下
var myArr = [
{coinId: "2",maxCNYAmount: "1000000.00",minCNYAmout: "100.00",referenceCoinCNYPrice: "8000",shortName: "aa"},
{coinId: "1",maxCNYAmount: "1000000.00",minCNYAmout: "100.00",referenceCoinCNYPrice: "8000",shortName: "bb"},
{coinId: "2",maxCNYAmount: "1000000.00",minCNYAmout: "100.00",referenceCoinCNYPrice: "8000",shortName: "aa"},
{coinId: "1",maxCNYAmount: "1000000.00",minCNYAmout: "100.00",referenceCoinCNYPrice: "8000",shortName: "bb"},
{coinId: "2",maxCNYAmount: "1000000.00",minCNYAmout: "100.00",referenceCoinCNYPrice: "8000",shortName: "aa"},
];
var obj= {};
myArr = myArr .reduce((item, next)=> {obj[next.coinId] ?'':obj[next.coinId] =true&& item.push(next);return item}, [])
console.log(myArr );
这样就可以了