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;
},
4

2 回答 2

5

一切都继承自ObjectJavascript,所以typeof返回object. 该方法确实返回一个数组,您可以使用以下方法进行验证:console.log(arrayOfObjects instanceof Array)

于 2013-06-30T10:11:41.133 回答
-1

您可以使用 jquery 的 $.each 方法来循环遍历对象。

于 2013-06-30T10:20:09.467 回答