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
} );
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With