0

I have an array of objects nodes, where each component object has .id property, and I would like to create a square matrix which is a nested array indexed by [id][id] representing node-by-node interactions:

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]);
});

In the console I am getting:

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

Why is the object is not assigned in the expression matrix[node.id] = ...? Why there is no error or warning? How can I fix it?


Upd: following @pilotcam explanation that forEach does not return a value, I tried following:

    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]);
});

Still my matrix is not getting filled in the inner loop:

...
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 [  ]
Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58
  • Seems to work for me. See https://jsfiddle.net/6gxvyncf/ – pilotcam May 08 '17 at 20:54
  • after [adding firebug plugin](http://stackoverflow.com/questions/17382200/print-var-in-jsfiddle), i get the same output as above with the fiddle ;( what output are you getting in the fiddle? Can you post it in your answer? – Dima Lituiev May 08 '17 at 21:04
  • the matrix does not seem to get modified at all within either of loops – Dima Lituiev May 08 '17 at 21:07

2 Answers2

2

The javascript forEach method does not return a value. You probably want to do

matrix[node.id] = [];

...and manipulate that inside the second forEach. From the question posed, I'm guessing you want something like this:

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]);
});

I modified the fiddle to loop through your hash table and show that it is probably doing what you want. https://jsfiddle.net/rtxbzove/

pilotcam
  • 1,749
  • 14
  • 22
1

The issue is that I try to index an array with non-integer values. The proper way seems to use an 'object' / hash table:

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);

Alternatively, with forEach loops:

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]);
});
Dima Lituiev
  • 12,544
  • 10
  • 41
  • 58