+ 1

Javascript array of objects - filter by uniqueness of specific key, and keep only certain keys

'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:"Matt", lname:"Will", age:"14"}, {id:"124", name:"Matt", lname:"Will", age:"17"}, {id:"124", name:"Matt", lname:"Will", age:"21"}, {id:"128", name:"Colson", lname:"Baker", age:"18"}, {id:"132", name:"Mike", lname:"Baker", 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:"Matt", lname:"Will"}, {id:"128", name:"Colson", lname:"Baker"}, {id:"132", name:"Mike", lname:"Baker"} ]; 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!!

14th Sep 2018, 9:49 PM
Enoch Ojukwu
1 Respuesta
+ 2
Instead of return a val, use map to return the required object Set cannot get unique objects directly from objects, Set only get unique array elements, need make it to json string and parse them back to objects after Set processed. https://code.sololearn.com/WUqIFOzGcQDq/?ref=app
15th Sep 2018, 1:36 AM
Calviղ
Calviղ - avatar