Filtering duplicate hashes from array of hashes - Javascript
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!
javascript arrays hash
|
show 3 more comments
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!
javascript arrays hash
2
Reduce the array to an object, using the id as key, and then convert back to array usingObject.values()
.
– Ori Drori
Dec 21 '18 at 9:44
What you tried?
– ZiTAL
Dec 21 '18 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
Dec 21 '18 at 9:46
Only pure JS allowed?
– hindmost
Dec 21 '18 at 9:49
@hindmost, yes, this is part of my react app
– Dende
Dec 21 '18 at 9:52
|
show 3 more comments
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!
javascript arrays hash
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!
javascript arrays hash
javascript arrays hash
edited Dec 21 '18 at 11:07
Kamil Kiełczewski
13.4k87297
13.4k87297
asked Dec 21 '18 at 9:40
DendeDende
103210
103210
2
Reduce the array to an object, using the id as key, and then convert back to array usingObject.values()
.
– Ori Drori
Dec 21 '18 at 9:44
What you tried?
– ZiTAL
Dec 21 '18 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
Dec 21 '18 at 9:46
Only pure JS allowed?
– hindmost
Dec 21 '18 at 9:49
@hindmost, yes, this is part of my react app
– Dende
Dec 21 '18 at 9:52
|
show 3 more comments
2
Reduce the array to an object, using the id as key, and then convert back to array usingObject.values()
.
– Ori Drori
Dec 21 '18 at 9:44
What you tried?
– ZiTAL
Dec 21 '18 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
Dec 21 '18 at 9:46
Only pure JS allowed?
– hindmost
Dec 21 '18 at 9:49
@hindmost, yes, this is part of my react app
– Dende
Dec 21 '18 at 9:52
2
2
Reduce the array to an object, using the id as key, and then convert back to array using
Object.values()
.– Ori Drori
Dec 21 '18 at 9:44
Reduce the array to an object, using the id as key, and then convert back to array using
Object.values()
.– Ori Drori
Dec 21 '18 at 9:44
What you tried?
– ZiTAL
Dec 21 '18 at 9:45
What you tried?
– ZiTAL
Dec 21 '18 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
Dec 21 '18 at 9:46
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
Dec 21 '18 at 9:46
Only pure JS allowed?
– hindmost
Dec 21 '18 at 9:49
Only pure JS allowed?
– hindmost
Dec 21 '18 at 9:49
@hindmost, yes, this is part of my react app
– Dende
Dec 21 '18 at 9:52
@hindmost, yes, this is part of my react app
– Dende
Dec 21 '18 at 9:52
|
show 3 more comments
4 Answers
4
active
oldest
votes
You can use reduce too
//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);
add a comment |
Try this
h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
Input array in h, time complexity 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));
add a comment |
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);
add a comment |
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 :)
1
arrayAssociative[array[item].id] || {}
this line gives value in left of||
if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
– Kamil Kiełczewski
Dec 21 '18 at 10:03
thanks for the explanation <3
– Stephan T.
Dec 21 '18 at 10:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53882375%2ffiltering-duplicate-hashes-from-array-of-hashes-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use reduce too
//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);
add a comment |
You can use reduce too
//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);
add a comment |
You can use reduce too
//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);
You can use reduce too
//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);
//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);
//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);
edited Dec 21 '18 at 11:50
answered Dec 21 '18 at 10:04
Just codeJust code
10.5k53267
10.5k53267
add a comment |
add a comment |
Try this
h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
Input array in h, time complexity 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));
add a comment |
Try this
h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
Input array in h, time complexity 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));
add a comment |
Try this
h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
Input array in h, time complexity 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));
Try this
h.filter(( t={}, a=>!(t[a.id]=a.id in t) ))
Input array in h, time complexity 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));
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 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));
edited Jan 14 at 3:41
answered Dec 21 '18 at 9:45
Kamil KiełczewskiKamil Kiełczewski
13.4k87297
13.4k87297
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
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);
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);
answered Dec 21 '18 at 10:03
zheng lizheng li
6917
6917
add a comment |
add a comment |
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 :)
1
arrayAssociative[array[item].id] || {}
this line gives value in left of||
if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
– Kamil Kiełczewski
Dec 21 '18 at 10:03
thanks for the explanation <3
– Stephan T.
Dec 21 '18 at 10:05
add a comment |
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 :)
1
arrayAssociative[array[item].id] || {}
this line gives value in left of||
if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
– Kamil Kiełczewski
Dec 21 '18 at 10:03
thanks for the explanation <3
– Stephan T.
Dec 21 '18 at 10:05
add a comment |
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 :)
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 :)
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 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);
edited Dec 21 '18 at 10:00
answered Dec 21 '18 at 9:52
Stephan T.Stephan T.
1,2821921
1,2821921
1
arrayAssociative[array[item].id] || {}
this line gives value in left of||
if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
– Kamil Kiełczewski
Dec 21 '18 at 10:03
thanks for the explanation <3
– Stephan T.
Dec 21 '18 at 10:05
add a comment |
1
arrayAssociative[array[item].id] || {}
this line gives value in left of||
if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099
– Kamil Kiełczewski
Dec 21 '18 at 10:03
thanks for the explanation <3
– Stephan T.
Dec 21 '18 at 10:05
1
1
arrayAssociative[array[item].id] || {}
this line gives value in left of ||
if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099– Kamil Kiełczewski
Dec 21 '18 at 10:03
arrayAssociative[array[item].id] || {}
this line gives value in left of ||
if it is not null/undefined/0/false or value in the right if opposit - stackoverflow.com/q/2100758/860099– Kamil Kiełczewski
Dec 21 '18 at 10:03
thanks for the explanation <3
– Stephan T.
Dec 21 '18 at 10:05
thanks for the explanation <3
– Stephan T.
Dec 21 '18 at 10:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53882375%2ffiltering-duplicate-hashes-from-array-of-hashes-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Reduce the array to an object, using the id as key, and then convert back to array using
Object.values()
.– Ori Drori
Dec 21 '18 at 9:44
What you tried?
– ZiTAL
Dec 21 '18 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
Dec 21 '18 at 9:46
Only pure JS allowed?
– hindmost
Dec 21 '18 at 9:49
@hindmost, yes, this is part of my react app
– Dende
Dec 21 '18 at 9:52