1

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?.

3
  • 1
    Just do console.log(result). It's just printing that because arrays are objects. Try running typeof [] to see what I mean. Commented May 10, 2016 at 20:59
  • In javascript, typeof([]) is object Commented May 10, 2016 at 20:59
  • You should check the values before checking the type :) Commented May 10, 2016 at 21:00

3 Answers 3

1

typeof in JavaScript is less useful than one might hope. Instead you can use:

console.log(Array.isArray(result));
Sign up to request clarification or add additional context in comments.

Comments

1

If you look at the documentation of typeof you will see that arrays always return "object" for the typeof operator. In javascript there is very little difference between an array and an object.

2 Comments

" In javascript there is no difference between an array and an object." , Dear God!,why?
Both mapped keys to values, you can use either array syntax or object syntax to access arrays and objects. If you think of objects having keys which are usually strings and arrays having keys which are usually numbers you can see they end up serving the same purpose, e.g. that of a Map from key to value.
0

The thing is - arrays in JavaScript are objects. They inherit different methods form Array constructor, but checking the type will return 'object'. You can quickly check if your result is an array with Array.isArray() method:

console.log(Array.isArray(result)); 

Or use a polyfill when working with legacy browsers:

function is_array(obj) {
    return  typeof obj === 'object' && obj.constructor === Array;
}

console.log(is_array(result)); 

2 Comments

same can be achieved with Array.isArray, which has the recommend polyfill of if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype.toString.call(arg) === '[object Array]'; }; } see developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
Right, that's much better. I was just checking a very old version of 'The Good Parts' ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.