I have an array of objects... (actually I'm not sure what I have but it looks like this)
list =
{
"ZIG": [
"CSK",
"DKR",
"CSK",
"YNA",
"CSK"
],
"ZKG": [
"YNA"
],
"ZND": [
"NIM",
"DKR",
"AJY"
],
"ZNE": [
"PHE",
"PER"
]
}
And I'm looking for a way to end up with
list =
{
"ZIG": [
"DKR",
"YNA",
"CSK"
],
"ZKG": [
"YNA"
],
"ZND": [
"NIM",
"DKR",
"AJY"
],
"ZNE": [
"PHE",
"PER"
]
}
I was able to remove most of the duplicates by using uniq but there are still some duplicates
1 Answer 1
With ES6 you can do something like this:
const list = { "ZIG": [ "CSK", "DKR", "CSK", "YNA", "CSK" ], "ZKG": [ "YNA" ], "ZND": [ "NIM", "DKR", "AJY" ], "ZNE": [ "PHE", "PER" ] }
const r = Object.entries(list).map(([k,v]) => ({[k]: Array.from(new Set(v))}))
console.log(...r)
Where you would get the entries of the object (via Object.entries) map each of it and then compose the new values via using new Set. Lastly just spread the resulting array to get the desired object result.
answered Nov 14, 2018 at 22:04
Akrion
18.6k1 gold badge38 silver badges58 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.