js中数组对象去重
Posted 尔嵘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js中数组对象去重相关的知识,希望对你有一定的参考价值。
需求:对如下的数组进行去重
let stay = [{key1: 10.223,key2: 0.225},{key1: 10.01112,key2: 0.225},{key1: 10.2231,key2: 0.226},{key1: 10.223,key2: 0.22513},{key1: 10.223,key2: 0.225},{key1: 10.2231,key2: 0.226}];
解决:
function handleRemove(val){
let arr = val.map(item=>JSON.stringify(item));
let result = Array.from(new Set(arr)).map((item)=>JSON.parse(item));
return result;
}
let stay = [
{
key1: 10.223,
key2: 0.225
},
{
key1: 10.01112,
key2: 0.225
},
{
key1: 10.2231,
key2: 0.226
},
{
key1: 10.223,
key2: 0.22513
},
{
key1: 10.223,
key2: 0.225
},
{
key1: 10.2231,
key2: 0.226
}];
let value = this.handleRemove(stay);
console.log(value);
最后结果:
以上是关于js中数组对象去重的主要内容,如果未能解决你的问题,请参考以下文章