<分区>
我对以下内容有点迷茫:
当我执行两个不同数组的 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);