admin管理员组文章数量:1404603
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 ofcb
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 evenconsole.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
3 Answers
Reset to default 3You 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]
.
本文标签: Javascript pushing objects into arrayStack Overflow
版权声明:本文标题:Javascript pushing objects into array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744840162a2627875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论