Skip to content

Instantly share code, notes, and snippets.

@AdventureBear
Last active August 29, 2015 14:21
Show Gist options
  • Save AdventureBear/60c82b3ea60c78be88d1 to your computer and use it in GitHub Desktop.
Save AdventureBear/60c82b3ea60c78be88d1 to your computer and use it in GitHub Desktop.
Pivot Object into new Arrays - hardcoded via @oab
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