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";
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";
Share
Improve this question
edited May 23, 2017 at 10:27
CommunityBot
11 silver badge
asked Dec 26, 2015 at 13:23
nxetnxet
7382 gold badges10 silver badges23 bronze badges
3
- The structure you want is invalid, you can't have named keys in arrays – adeneo Commented Dec 26, 2015 at 13:25
- There's absolutely no way to do so in JS? – nxet Commented Dec 26, 2015 at 13:26
- All keys in arrays are / must be integers in javascript – Norbert Commented Dec 26, 2015 at 13:42
2 Answers
Reset to default 9You can use object or Map (ES6) data structure to achieve this, e.g.:
var single = {};
for (var i = 0; i < all.length; i++) {
name = all[i].name;
if (!single[name]) {
single[name] = [];
}
single[name].push(all[i]);
}
the result (single
object) looks like:
{
name1: [node1, node2, node3],
name2: [node1, node2, node3, node4, node5, node6],
name2: [node1, node2]
}
Javascript uses objects and arrays. There is no multidimensional-array.
The structure you want would look like this in javascript:
{
name1: [a,b,c],
name2: [d,e,f]
}
In fact in JS, you can just define a variable with such a object like this (object literal):
var myObject = {
name1: [a,b,c],
name2: [d,e,f]
}
You do not need to iterate to construct an object in JS, object-literals are easy to understand and fast.
If for some reason you need to map some data to this new format you want, I personally would use methods such as the Array.prototype.reduce.
myMap = all.reduce(function (result, item) {
result[item.name] = (result[item.name] || []).push(item);
return result;
},{});