I have an array of hashes, like this:
[{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"}]
I want to throw out duplicate hashes. Set doesn't work because hashes are unique objects.
I feel stuck and need a kick to think. Please advise!
I have an array of hashes, like this:
[{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"}]
I want to throw out duplicate hashes. Set doesn't work because hashes are unique objects.
I feel stuck and need a kick to think. Please advise!
Share Improve this question edited Dec 21, 2018 at 11:07 Kamil Kiełczewski 92.5k34 gold badges395 silver badges370 bronze badges asked Dec 21, 2018 at 9:40 DendeDende 5946 silver badges22 bronze badges 8-
2
Reduce the array to an object, using the id as key, and then convert back to array using
Object.values()
. – Ori Drori Commented Dec 21, 2018 at 9:44 - What you tried? – ZiTAL Commented Dec 21, 2018 at 9:45
- You would have to loop through the array for each possible hash and check, remove. Or change the data format, use an object with the hash string as a key and the name and whatever else as a separate object for the value – Patrick Evans Commented Dec 21, 2018 at 9:46
- Only pure JS allowed? – hindmost Commented Dec 21, 2018 at 9:49
- @hindmost, yes, this is part of my react app – Dende Commented Dec 21, 2018 at 9:52
4 Answers
Reset to default 7You can use reduce too
//I added ma 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);
Try this
h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
Input array in h, time plexity O(n), explanation 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));
Space for time
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);
I would suggest an approach with associative arrays, this makes duplicate removal easier. If you can, you should build your array as an associative array in the first place, so that you don't have to convert it. Here is how you do it:
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);
I don't know the exact reason, why the line
var element = arrayAssociative[id] =arrayAssociative[id] || {};
works for this, but let's just accept the funcitonality as it is :)