Last active
August 29, 2015 14:21
-
-
Save AdventureBear/60c82b3ea60c78be88d1 to your computer and use it in GitHub Desktop.
Pivot Object into new Arrays - hardcoded via @oab
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 obj = [ | |
{ | |
"name": "Free Code Camp", | |
"cost": 0, | |
"weeks": 18, | |
"location": "online" | |
}, | |
{ | |
"name": "Costly Code Camp", | |
"cost": 20000, | |
"weeks": 12, | |
"location": "Chicago" | |
}, | |
{ | |
"name": "Boring Camp Coders", | |
"cost": 5000, | |
"weeks": 20, | |
"location": "Panama" | |
}, | |
]; | |
dataArray = [ | |
{ names: [] }, | |
{ costs: [] }, | |
{ weeks: [] }, | |
{ locations: [] } | |
]; | |
// I want my array to look like this: | |
// [ { names: ['Free Code Camp', 'Costly Code Camp', 'Boring Code Camp'] }, | |
// { weeks: [18, 12, 20] }, | |
// { cost: [0, 20000, 5000] } ] | |
for (var i=0; i<obj.length; i++) { | |
dataArray[0].names.push(obj[i].name); | |
dataArray[1].costs.push(obj[i].cost); | |
dataArray[2].weeks.push(obj[i].weeks); | |
dataArray[3].locations.push(obj[i].location); | |
} | |
console.log(dataArray[3]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment