我有一系列的哈希,就像这样:
[{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c6941735", name: "skandi"},
{id: "4bf58dd8d48988d147941735", name: "diner"},
{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d14a941735", name: "vietnam"},
{id: "4bf58dd8d48988d1ce941735", name: "fish"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"}]
我想扔掉重复的哈希。设置无效,因为哈希是唯一的对象。
我感到被困住了,需要踢一下去思考。请指教!
答案 0 :(得分:7)
您也可以使用reduce
//I added comma to each object
const data= [{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c6941735", name: "skandi"},
{id: "4bf58dd8d48988d147941735", name: "diner"},
{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d14a941735", name: "vietnam"},
{id: "4bf58dd8d48988d1ce941735", name: "fish"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"}]
const result= data.reduce((current,next)=>{
if(!current.some(a=> a.name === next.name)){
current.push(next);
}
return current;
},[])
console.log(result);
答案 1 :(得分:5)
尝试一下
h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
以h为单位的输入数组,时间复杂度 O(n),解释here .
let h = [{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c6941735", name: "skandi"},
{id: "4bf58dd8d48988d147941735", name: "diner"},
{id: "4bf58dd8d48988d110941735", name: "italy"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d14a941735", name: "vietnam"},
{id: "4bf58dd8d48988d1ce941735", name: "fish"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"},
{id: "4bf58dd8d48988d1c4941735", name: "resto"}]
let t; // declare t to avoid use global (however works without it too)
let r= h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
console.log(JSON.stringify(r));
答案 2 :(得分:3)
时间充裕
let arr = [
{ id: '4bf58dd8d48988d110941735', name: 'italy' },
{ id: '4bf58dd8d48988d1c6941735', name: 'skandi' },
{ id: '4bf58dd8d48988d147941735', name: 'diner' },
{ id: '4bf58dd8d48988d110941735', name: 'italy' },
{ id: '4bf58dd8d48988d1c4941735', name: 'resto' },
{ id: '4bf58dd8d48988d14a941735', name: 'vietnam' },
{ id: '4bf58dd8d48988d1ce941735', name: 'fish' },
{ id: '4bf58dd8d48988d1c4941735', name: 'resto' },
{ id: '4bf58dd8d48988d1c4941735', name: 'resto' }
]
let map = {};
let rest = arr.filter((item) => {
if(map[item.id] === void 0) {
map[item.id] = item.id;
return true;
}
});
map = null;
console.log(rest);
答案 3 :(得分:2)
我建议使用关联数组的方法,这使重复删除变得更容易。如果可以的话,首先应该将数组构建为关联数组,这样就不必转换它。这是您的操作方式:
var array = [{
id: "4bf58dd8d48988d110941735",
name: "italy"
},
{
id: "4bf58dd8d48988d1c6941735",
name: "skandi"
}, {
id: "4bf58dd8d48988d147941735",
name: "diner"
}, {
id: "4bf58dd8d48988d110941735",
name: "italy"
}, {
id: "4bf58dd8d48988d1c4941735",
name: "resto"
}, {
id: "4bf58dd8d48988d14a941735",
name: "vietnam"
}, {
id: "4bf58dd8d48988d14a941735",
name: "fish"
}, {
id: "4bf58dd8d48988d1c4941735",
name: "resto"
}, {
id: "4bf58dd8d48988d1c4941735",
name: "resto"
}
];
// you can access the array with arrayAssociative[id], where the id is the real id like "4bf58dd8d48988d110941735"
var arrayAssociative = {};
for (item in array) {
// first get the unique id's
var addedNode = arrayAssociative[array[item].id] = arrayAssociative[array[item].id] || {};
if (addedNode.names == null)
addedNode.names = {};
// now get the unique names
var addedName = arrayAssociative[array[item].id].names[array[item].name] = arrayAssociative[array[item].id].names[array[item].name] || {};
}
console.log(arrayAssociative);
我不知道确切的原因,为什么行
var元素= arrayAssociative [id] = arrayAssociative [id] || {};
可以做到这一点,但让我们直接接受其功能性:)