Skip to content

Instantly share code, notes, and snippets.

@naranjja
Created July 5, 2018 03:25
Show Gist options
  • Save naranjja/68011782df3ebeb79f88b3e4e39a5819 to your computer and use it in GitHub Desktop.
Save naranjja/68011782df3ebeb79f88b3e4e39a5819 to your computer and use it in GitHub Desktop.
Map array and object of arrays in JavaScript
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