2

I have one question. Why this method return object? In other part of my code i cant use forEach method.

objectToArrayOfObjects: function (obj, additionalProperties) {
    var arrayOfObjects = [];
    for (var name in obj) {
        if (obj.hasOwnProperty(name)) {
            var generatedObject = {name: name, value: obj[name]};
            arrayOfObjects.push(_.extend(generatedObject, additionalProperties));
        }
    }
    console.log(typeof arrayOfObjects); // return object
    return arrayOfObjects;
},
1
  • 2
    typeof [] === 'object'; because Arrays are essentially Objects in JavaScript. Commented Jun 30, 2013 at 10:11

2 Answers 2

5

Everything inherits from Object in Javascript, so typeof returns object. The method does return an array, you can verify that by using: console.log(arrayOfObjects instanceof Array)

Sign up to request clarification or add additional context in comments.

2 Comments

Or better - consider the array comming from another frame the above example would be false but looking at the constructors name instead would be ok: Object.prototype.toString.call(arrayOfObjects) === '[object Array]'
Or for that matter arrayOfObjects.constructor === Array, or /array/i.test(arrayOfObjects.constructor)
-1

you may use $.each method of jquery to loop through the object.

1 Comment

This doesn't answer the question. Also, no need to use jQuery for something that could be accomplished in two lines of code.

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.