I was given an array such as this :
data = [
['first_name', 'second_name', 'sex'],
['Jacob', 'David', 'M'],
['Kathryn', 'Gardener', 'F'],
['Jamaal', 'Dave', 'Unknown']
].
the solution is to return the array as an object looking something like this :
[
{ first_name: "Jacob", second_name: "David", sex: "M" },
{ first_name: "Kathryn", second_name: "Gardener", sex: "F" }
{....}
];
I wrote the following code :
var object = {};
var arrays = [];
for (i = 0; i < data.length; i++){
for (var j = i; j < data.length; j++){
var keys = data[0][i];
var values = data [j][i];
object[keys] = values;
}
arrays.push(object);
};
but the output I'm having is :
[
{
first_name :'Jamaal',
second_name : 'Dave',
sex : 'unknown'
},
{
first_name :'Jamaal',
second_name : 'Dave',
sex : 'unknown'
},
{
first_name :'Jamaal',
second_name : 'Dave',
sex : 'unknown'
},
{
first_name :'Jamaal',
second_name : 'Dave',
sex : 'unknown'
}
];
I've also tried several ways either i end up with being able to create only one object...
data[0][i]as a keys.var object = {};