'Javascript array of objects - filter by uniqueness of specific key, and keep only certain keys
I'm sure this question has been asked in some form here before, but I am having trouble finding the right solution. I have an array of objects like this:
[
{id:"124", name:"Joe", lname:"Smith", age:"14"},
{id:"124", name:"Joe", lname:"Smith", age:"17"},
{id:"124", name:"Joe", lname:"Smith", age:"21"},
{id:"128", name:"Tom", lname:"Cans", age:"18"},
{id:"132", name:"Mik", lname:"Brak", age:"21"}
];
and I would like to shorten this array so that it only includes objects with unique id keys, as well as only the keys id, name, lname. The output I'm going for then is:
[
{id:"124", name:"Joe", lname:"Smith"},
{id:"128", name:"Tom", lname:"Cans"},
{id:"132", name:"Mik", lname:"Brak"}
];
What I've done so far - I can use the following code snippet to create an array of unique IDs, but that isn't getting me to where I need to go:
let uniqueIDs = [... new Set(mydata.map(val => val.id))];
Any help is appreciated!!
Solution 1:[1]
Store your array items in an object, indexed by id, and skip any which you already have:
var obj = {}
[ {id:"124", name:"Joe", lname:"Smith", age:"14"},
{id:"124", name:"Joe", lname:"Smith", age:"17"},
{id:"124", name:"Joe", lname:"Smith", age:"21"},
{id:"128", name:"Tom", lname:"Cans", age:"18"},
{id:"132", name:"Mik", lname:"Brak", age:"21"}
].forEach(function(d){
if ( ! obj[d.id] ) {
// omit the info you're not interested in
obj[d.id] = { id: d.id, name: d.name, lname: d.lname }
}
})
var uniq = Object.values(obj);
console.log(uniq)
Solution 2:[2]
You could take a Set of JSON strings and map them by parsing it.
var array = [{ id: "124", name: "Joe", lname: "Smith", age: "14" }, { id: "124", name: "Joe", lname: "Smith", age: "17" }, { id: "124", name: "Joe", lname: "Smith", age: "21" }, { id: "128", name: "Tom", lname: "Cans", age: "18" }, { id: "132", name: "Mik", lname: "Brak", age: "21" }],
unique = Array.from(
new Set(array.map(({ id, name, lname }) => JSON.stringify({ id, name, lname }))),
JSON.parse
);
console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Solution 3:[3]
First step is to create a data structure where you can be sure your entries are unique. I like reduce to convert an array to a map.
const arrayToMap = array.reduce((obj, { id, name, lname}) => ({ ...obj, [id]: { id, name, lname }), {});
Then you need to build the map to an array by iterating over the keys.
const mapToArray = Object.keys(arrayToMap).map(key => ({ ...arrayToMap[key] });
Solution 4:[4]
Here's an approach using Set and filter to remove duplicate id keys and map to remove the age key:
const data = [
{id: "124", name: "Joe", lname: "Smith", age: "14"},
{id: "124", name: "Joe", lname: "Smith", age: "17"},
{id: "124", name: "Joe", lname: "Smith", age: "21"},
{id: "128", name: "Tom", lname: "Cans", age: "18"},
{id: "132", name: "Mik", lname: "Brak", age: "21"},
];
const ids = new Set(data.map(e => e.id));
const result = data
.filter(e => ids.delete(e.id))
.map(({id, name, lname}) => ({id, name, lname}))
;
console.log(result);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | i alarmed alien |
| Solution 2 | Nina Scholz |
| Solution 3 | |
| Solution 4 |
