我对以下内容有些困惑:
当我使用console.log输出两个不同的数组时,其中一个给出了实际长度,而另一个则没有。
第一个数组的输出,长度正确:
[Object, Object, Object]
0: Object
1: Object
2: Object
length: 3
__proto__: Array[0]
第二个的输出,长度应该是4,但实际上是0:
[A: Object, B: Object, C: Object, D: Object]
A: Object
B: Object
C: Object
D: Object
length: 0
__proto__: Array[0]
为什么我的第一个数组有正确的长度,但第二个数组没有?
编辑:这是生成上述输出的代码:
var links = [
{source: "A", target: "B"},
{source: "A", target: "C"},
{source: "A", target: "D"}
];
var nodes = [];
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});
console.log(links);
console.log(nodes);