我有一系列哈希,这样:
[{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"}]
我想扔掉重复的哈希。设置不起作用,因为哈希是唯一的对象。
我感到卡住了,需要踢思考。请建议!
您可以使用redail
//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);
尝试此
h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
输入数组在h,时间复杂性 o(n),在此处说明。
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));
时间的空间
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);
我建议使用关联数组的方法,这使重复的删除更加容易。如果可以的话,您应该首先将数组构建为关联阵列,以便您不必将其转换。这是您的方式:
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] ||{};
为此工作,但让我们接受它是:)