【问题标题】:d3 js: creating a nested array (matrix) from a flat arrayd3 js:从平面数组创建嵌套数组(矩阵)
【发布时间】:2017-05-08 19:28:47
【问题描述】:

我有一个对象数组nodes,其中每个组件对象都有.id 属性,我想创建一个正方形matrix,它是一个由[id][id] 索引的嵌套数组,表示逐个节点互动:

nodes.forEach(function(node, i) {
    matrix[node.id] = nodes.forEach(
        function(node1, j) {
            console.log(i,j);
            return {
                "x": j,
                "y": i,
                "z": 0
            };
    });
    console.log(i, matrix[node.id]);
});

在我得到的控制台中:

...
149 1
...  
149 148  
149 149  
149 undefined

为什么在表达式matrix[node.id] = ...中没有赋值对象?为什么没有错误或警告?我该如何解决?


更新:在@pilotcam 解释forEach 没有返回值之后,我尝试了以下操作:

    var matrix = [];
var testnodes = [{id: "aaa", a:10},
                 {id: "aab", a:20},
                 {id: "aac", a:30},
                 {id: "aba", a:40}]

testnodes.forEach(function(node, i) {
    matrix[node.id] = [];
   // [{x: 1, y:2, z:0}, {x:2,y:3,z:0}];
    testnodes.forEach(
        function(node1, j) {
            matrix[node.id][node1.id] = {
                x: j,
                y: i,
                z: 0
            };
        console.log(i,j, node.id, node1.id, matrix[node.id][node1.id]);
    });
    console.log(i, matrix[node.id]);
});

我的matrix 仍然没有被填充到内部循环中:

...
3 1 aba aab Object { x: 1, y: 3, z: 0 }
3 2 aba aac Object { x: 2, y: 3, z: 0 }
3 3 aba aba Object { x: 3, y: 3, z: 0 }
3 Array [  ]

【问题讨论】:

  • 似乎对我有用。见jsfiddle.net/6gxvyncf
  • adding firebug plugin 之后,我用小提琴得到了与上面相同的输出;(你在小提琴中得到了什么输出?你能把它贴在你的答案中吗?
  • 矩阵似乎在任何一个循环中都没有被修改

标签: javascript d3.js matrix


【解决方案1】:

javascript forEach 方法不返回值。你可能想这样做

matrix[node.id] = [];

...并在第二个forEach 中对其进行操作。从提出的问题来看,我猜你想要这样的东西:

nodes.forEach(function(node, i) {
    matrix[node.id] = [];
    nodes.forEach(
        function(node1, j) {
            console.log(i,j);
            matrix[node.id][node1.id] = {
                "x": j,
                "y": i,
                "z": 0
            };
    });
    console.log(i, matrix[node.id]);
});

我修改了小提琴以循环遍历您的哈希表,并表明它可能正在执行您想要的操作。 https://jsfiddle.net/rtxbzove/

【讨论】:

  • 谢谢!有道理,但还是不行,看更新
【解决方案2】:

问题是我尝试用非整数值索引一个数组。正确的方法似乎是使用“对象”/哈希表:

var matrix = {};
var testnodes = [{id: "aaa", a:10},
                 {id: "aab", a:20},
                 {id: "aac", a:30},
                 {id: "aba", a:40}]

// with simple for loops:
for (var i = 0, len = testnodes.length; i < len; i++) {
  matrix[testnodes[i].id] = {};
  for (var j = 0, len = testnodes.length; j < len; j++) {
    matrix[testnodes[i].id][testnodes[j].id] =  {
                x: j,
                y: i,
                z: 0
            };
  }
  console.log( "matrix:", matrix[testnodes[i].id] );
}
console.log( "matrix:", matrix);

或者,使用forEach 循环:

testnodes.forEach(function(node, i) {
    matrix[node.id] = {};
    testnodes.forEach(
        function(node1, j) {
            console.log(i,j);
            matrix[node.id][node1.id] = {
                "x": j,
                "y": i,
                "z": 0
            };
    });
    console.log(i, matrix[node.id]);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-04
    • 2015-05-08
    • 1970-01-01
    • 2013-01-27
    • 2022-11-24
    • 1970-01-01
    • 2013-10-21
    • 2015-10-21
    相关资源
    最近更新 更多