Reputation: 6735
I have an object with data formatted similar to the following...
var origData = {data: [
['Header1', 'Header2', 'Header3'],
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]};
What I'd like to do is reformat this such that the data is structured like this...
newData = {
{'Header1': 0, 'Header2': 1, 'Header3': 2},
{'Header1': 3, 'Header2': 4, 'Header3': 5},
{'Header1': 6, 'Header2': 7, 'Header3': 8}
};
Every time I try to write a possible solution, I get lost in nested for
loops. I have a feeling I'm over-complicating this.
The header array is origData.data[0]
.
The data arrays can be retrieved using:
dataArrays = [];
for (var i = 1; i < origData.data.length; i++) {
dataArrays.push(origData.data[i]);
}
At this point, this is where I start getting confused. Basically, I need to construct a new object in a for
loop that contains the header array elements as the keys, and the respective data array elements as the values.
Any help would be greatly appreciated. Thank you!
Upvotes: 0
Views: 81
Reputation: 7117
var origData = {data: [
['Header1', 'Header2', 'Header3'],
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]};
var result = [];
for (i=1; i< origData.data.length; i++){
var obj = {};
obj[origData.data[0][0]]=origData.data[i][0];
obj[origData.data[0][1]]=origData.data[i][1];
obj[origData.data[0][2]]=origData.data[i][2];
result.push(obj);
}
alert(JSON.stringify(result));
Upvotes: 0
Reputation: 2002
You know the header element is always at offset 0
in origData
, you also know that in each array in the origData
parent array, there are 3 items at indexes 0
, 1
, and 2
. Therefore you only need one loop, starting from index 1
which creates a new object referencing back to origData
offset 0
for the header at each index and then the current iteration's object for the values.
Try this:
var newData = new Array(),
obj;
for (var i=1; i<origData.data.length; i++) {
obj = new Object();
obj[origData.data[0][0]] = origData.data[i][0];
obj[origData.data[0][1]] = origData.data[i][1];
obj[origData.data[0][2]] = origData.data[i][2];
newData.push(obj);
}
Upvotes: 0
Reputation: 700372
The result should be an array of object rather than an object of objects:
newData = [
{'Header1': 0, 'Header2': 1, 'Header3': 2},
{'Header1': 3, 'Header2': 4, 'Header3': 5},
{'Header1': 6, 'Header2': 7, 'Header3': 8}
];
Here is how you can create it:
var origData = {data: [
['Header1', 'Header2', 'Header3'],
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]};
var newData = [];
// get the headers
var headers = origData.data[0];
// loop through the original data arrays
for (var i = 1 ; i < origData.data.length; i++) {
// get the data array for this item
var data = origData.data[i];
// create an empty item
var item = {};
// combine headers with values into the item
for (var j = 0; j < headers.length; j++) {
item[headers[j]] = data[j];
}
// add the item to the array
newData.push(item);
}
// Show result in StackOverflow snippet
document.write(JSON.stringify(newData));
Upvotes: 1
Reputation: 1785
var headers = origData.data.shift();
var newData = [];
origData.data.forEach(function (arr) {
var obj = {};
arr.forEach(function (val, i) {
obj[headers[i]] = val;
});
newData.push(obj);
})
console.log('newData',newData);
Upvotes: 0
Reputation: 32047
Here's a quick (untested) stab at attacking your problem:
var origData = {data: [
['Header1', 'Header2', 'Header3'],
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]};
var headers = origData.data[0];
var i, j;
var result = [];
var row, item;
// start from the row after the headers
for (i = 1; i < origData.data.length; i++) {
row = origData.data[i];
item = {}
// assume that we've got an entry for each header in the individual rows
for (j = 0; j < headers.length; j++) {
item[headers[j]] = row[j];
}
result.push(item);
}
Upvotes: 0