最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript pushing objects into array - Stack Overflow

programmeradmin4浏览0评论
var cb = [];
for (var i = 0; i < 10; i++) {
    cb.push({
        'test': 'value'
    });
    console.log(cb);
};

I'm expecting to get: [{test: value}, {test: value}, ... , {test: value}]

What I end up getting is the final result at every log statement:

[Object]

[Object, Object]

[Object, Object, Object]

[Object, Object, Object, Object]

[Object, Object, Object, Object, Object]

[Object, Object, Object, Object, Object]

[Object, Object, Object, Object, Object, Object]

..........

When I expand any of those arrays they all have the same result. For example, the first array contains:

[{test: value}, {test: value}, ... , {test: value}] 

which is the final value, shouldn't it just have 1 object? The final result is what I expect, but I'm just confused about why after the first push the array has 10 elements. Can someone please explain what's going on?

var cb = [];
for (var i = 0; i < 10; i++) {
    cb.push({
        'test': 'value'
    });
    console.log(cb);
};

I'm expecting to get: [{test: value}, {test: value}, ... , {test: value}]

What I end up getting is the final result at every log statement:

[Object]

[Object, Object]

[Object, Object, Object]

[Object, Object, Object, Object]

[Object, Object, Object, Object, Object]

[Object, Object, Object, Object, Object]

[Object, Object, Object, Object, Object, Object]

..........

When I expand any of those arrays they all have the same result. For example, the first array contains:

[{test: value}, {test: value}, ... , {test: value}] 

which is the final value, shouldn't it just have 1 object? The final result is what I expect, but I'm just confused about why after the first push the array has 10 elements. Can someone please explain what's going on?

Share Improve this question edited Mar 9, 2016 at 17:23 Mike Cluck 32.5k13 gold badges83 silver badges94 bronze badges asked Mar 9, 2016 at 17:19 Milan DimicMilan Dimic 231 gold badge3 silver badges6 bronze badges 4
  • 2 console.log shows the state of cb now, and not at the time it was logged. – Siguza Commented Mar 9, 2016 at 17:21
  • Possible dublicate: stackoverflow./questions/750486/… – Bjørn Sørensen Commented Mar 9, 2016 at 17:21
  • 2 Try doing console.log(JSON.stringify(cb)) and you'll see what @Siguza is saying. Or even console.log(cb.length) will show you that the length is increasing correctly. – Mike Cluck Commented Mar 9, 2016 at 17:22
  • The log does not show the exact state of an array/object when you log it. stackoverflow./questions/4057440/… – epascarello Commented Mar 9, 2016 at 17:37
Add a ment  | 

3 Answers 3

Reset to default 3

You need to serialize yor output. Try:

var cb = [];
for (var i = 0; i < 10; i++) {
    cb.push({
        'test': 'value'
    });
    console.log(JSON.stringify(cb));
};

You are logging the array object, not any aspects of the object. The output is correct and so is the array. You just need to be more specific. If you set a breakpoint into the code where your console.log line is, you'll see that the array is being populated correctly.

And, if you don't want to see the object's state as it's being built, just move the log outside of the loop.

Doing cb[test]="value" (or maybe cb[myObject]="value"? I'm not sure what you want) would have returned what you expected : an array whose only key test (or myObject) contains the defined value.

When using push instead, you use the array not as an associative array (key = value) but rather as an indexed array (index = value), and push's contract is to add to the end of the array, using the next available index.
So now cb[0] is {test: "value"} and so are the others cb[1] to cb[9].

发布评论

评论列表(0)

  1. 暂无评论