Where exactly is wrong here? I get a "Cannot read property 'push' of undefined" error, and I believe that's because the array can't be set to multidimensional by simply declaring it in the loop.
var single = [];
for (var i = 0; i < all.length; i++) {
name = all[i].name;
single[name].push(all[i]);
}
What I'm trying to achieve is this structure:
Array (
([name1] => [node1],[node2],[node3]),
([name2] => [node1],[node2],[node3],[node4],[node5],[node6]),
([name2] => [node1],[node2])
etc...
)
I've tried searching here on SO, but so far only got two options:
Option 1: Shorthand in declaring the variable, aka [[]], which doesn't work.
var single = [[]];
Option 2: link Add another loop to work out the array before filling it.
var matrix = []
cols = 3;
//init the grid matrix
for ( var i = 0; i < cols; i++ ) {
matrix[i] = [];
}
I'd rather find a shorter solution, also because my array elements MUST have the key, while on the solution above they are numbered.
EDIT: since keys in JS array are not an option, would an object do the trick in this case? something like:
var obj = {
key1: value1,
key2: value2
};
obj.key3 = "value3";