Created
July 5, 2018 03:25
-
-
Save naranjja/68011782df3ebeb79f88b3e4e39a5819 to your computer and use it in GitHub Desktop.
Map array and object of arrays in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = [ | |
"2018", | |
"2017", | |
"2016" | |
] | |
let y = { | |
"2018": [ | |
1, | |
2, | |
3 | |
], | |
"2017": [ | |
4, | |
5 | |
], | |
"2016": [ | |
6 | |
] | |
} | |
const result = x.map(value => { | |
let data = [] | |
Object.keys(y).forEach(key => { | |
if (value == key) { | |
data.push(y[key]) | |
} | |
}) | |
return { | |
name: value, | |
data: data, | |
} | |
}) | |
console.log(JSON.stringify(result, null, 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment