Created
July 7, 2017 17:15
-
-
Save viniazvd/4ddf2ea8ee06f286bfb21adcf8b37f1e to your computer and use it in GitHub Desktop.
flatten array obj
This file contains 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
var arr = [ | |
[ | |
{ "name":"Distri Equipamente Cirurgicos", "data":18 }, | |
{ "name":"DNAPet", "data":70 } | |
], | |
[ | |
{ "name":"Distri Equipamente Cirurgicos", "data":62 }, | |
{ "name":"Medical Pet&Thingss", "data":184 }, | |
{ "name":"DNAPet", "data":251.2 } | |
], | |
[ | |
{ "name":"DNAPet", "data":2087.1 } | |
], | |
[ | |
{ "name":"Medical Pet&Thingss", "data":80 } | |
] | |
] | |
const flatten = ( acc, item ) => acc.concat( item ) | |
const array = arr.reduce( flatten ) | |
// console.log( array ) | |
const result = array.reduce ( ( acc, item ) => { | |
acc[item.name] = acc[item.name] ? acc[item.name] : [] | |
acc[item.name].push({ | |
data: item.data | |
}) | |
return acc | |
},{}) | |
console.log( JSON.stringify(result, null, 2) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment