I have this data stored in mongodb
{
"_id" : "RLvWTcsrbRXJeTqdB",
"examschoolid" : "5FF2JRddZdtTHuwkx",
"examsubjects" : [
{
"subject" : "Z4eLrwGwqG4pw4HKX"
},
{
"subject" : "fFcWby8ArpboizcT9"
}
],
"examay" : "NrsP4srFGfkc5cJkz",
"examterm" : "5A5dNTgAkdRr5j53j",
"examclass" : "gYF2wE4wBCRy9a3ZC",
"examname" : "First",
"examdate" : ISODate("2016-05-07T22:41:00Z"),
"examresultsstatus" : "notreleased"
}
and i am more interested in this part
"examsubjects" : [
{
"subject" : "Z4eLrwGwqG4pw4HKX"
},
{
"subject" : "fFcWby8ArpboizcT9"
}
],
I am trying to fetch the part above and convert it into an array that looks like this
[{
"Z4eLrwGwqG4pw4HKX" : "0",
"fFcWby8ArpboizcT9" : "0"
}],
The values from above become the keys that form the new array.I am using this code
console.log(doc.examsubjects);
var result = new Array();
for (i = 0; i < doc.examsubjects.length; i++) {
var arr = [];
for (var prop in doc.examsubjects[i]) {
arr.push(doc.examsubjects[i][prop]);
}
result.push(arr);
console.log(typeof result);//object
}
After running the code,console.log(typeof result);//object
result is still an object.How can i fix this?.
console.log(result). It's just printing that because arrays are objects. Try runningtypeof []to see what I mean.object