0

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

asked Nov 14, 2018 at 21:28

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.