Reputation:
How can I create a multidimensional array in nodejs? And push data in it?
How can I get an old-fashioned array inside an array, like this:
array{ sub_array1[(key1, value1)(key2,value2)],sub_array2[(key1, value1)(key2,value2)]}
So far I have tried lots of combinations of:
array.push()
okie., as per answered by rsp ., i have managed to get it like this
out_array.push( { 'key1' :value1,
'key2' : value2 ,
'key3': value3,
'key4': value4,
'key5': value5
} );
Upvotes: 3
Views: 30748
Reputation: 111336
There are no multidimensional arrays in JavaScript in the strict sense, but you can create an array that has other arrays as elements. The same with objects - I add it because it seems that what you want is objects and not arrays, or maybe a mixed structure with objects and arrays.
let arrayOfArrays = [[1, 2, 3], ['x', 'y', 'z']];
let objectOfObjects = { a: { x: 1, y: 2 }, b: { x: 3, y: 4 } };
let arrayOfObjects = [{ x: 1, y: 2 }, { x: 3, y: 4 }];
let objectOfArrays = { a: [1, 2, 3], b: ['x', 'y', 'z'] };
You access elements like this:
arrayOfArrays[1][2] === 'z';
objectOfObjects.b.x === 3;
arrayOfObjects[1].y === 4;
objectOfArrays.a[2] === 3;
Note that objectOfObjects.b.x
is the same as objectOfObjects['b']['x']
but shorter. Usually you use the bracket syntax when you have the key name in a variable, like this:
let key1 = 'b';
let key2 = 'x';
objectOfObjects[key1][key2] === objectOfObjects.b.x;
Upvotes: 8
Reputation: 11
Look at my small examle:
let obj = {
foo: [1, 2, 3, 4, 5, 6]
};
Object.assign(obj, {
bar: true
});
console.log(obj);
obj.foo.push(7);
console.log(obj);
for (var key in obj) {
console.log(key + '=' + obj[key]);
}
Upvotes: 0