简体   繁体   中英

Adding array of arrays dynamically in javascript

I am trying to add array of arrays dynamically in javascript. I need the data in the following format -

var dataArray = [[],[],[],.....,[]];

How can I initialize this kind of array? Suppose if I have three arrays to be added, I can initialize as follows -

var dataArray = [[],[],[]];

This will accept only three records to be added. But, what should I do in case of adding large number of arrays? Here I cannot know the amount of data I get as input. I have tried using concat() and merge() methods, these are adding contents directly in to a single array, but that is not what I wanted. Can any one please help me out on this?

You can build or add an array into an array like this:

var dataArray = [];
dataArray.push([1,2,3]);
dataArray.push([3,4,5]);

console.log(dataArray);   // [[1,2,3], [3,4,5]]

Or, if you want to add elements to the sub-arrays:

var dataArray = [];
dataArray.push([1,2,3]);
dataArray.push([3,4,5]);

dataArray[0].push(4);
dataArray[1].push(9);
console.log(dataArray);   // [[1,2,3,4], [3,4,5,9]]

You initialize a sub-array by assigning an array to the element of the outer array. You can then use array operations directly on the sub-array element:

// create a sub-array element
dataArray[2] = [];
dataArray[2].push(8);
dataArray[2].push(7);
console.log(dataArray[2]);  // [8,7]
console.log(dataArray);     // [[1,2,3,4], [3,4,5,9], [8,7]]

The key thing it appears you don't understand is that an array of arrays is just that. It's an outer array where each element in the outer array is itself an array. You use ordinary array methods to operate on either the outer array or the inner arrays. To operate on an inner array, you fetch that element from the outer array and then just treat it as an array. For example:

var dataArray = [];
dataArray.push([1,2,3]);
dataArray.push([3,4,5]);
console.log(dataArray);   // [[1,2,3], [3,4,5]]

var innerArray = dataArray[0];
console.log(innerArray);   // [1,2,3]
innerArray.push(12);
console.log(innerArray);   // [1,2,3,12]
innerArray.legnth = 2;
console.log(innerArray);   // [1,2]
innerArray.push(9,8,7);
console.log(innerArray);   // [1,2,9,8,7]
innerArray.splice(1,2);
console.log(innerArray);   // [1,8,7]

You have wrote "I am trying to add array of arrays dynamically in javascript "
The simple way is using Array.prototype.push method:

var arr1 = [1,2], arr2 = [3,4], arr3 = [5,6], arr4 = [7,8], arr5 = [9,10],
    dataArray = [];

[].push.apply(dataArray, [arr1, arr2, arr3, arr4, arr5]);
console.log(JSON.stringify(dataArray, 0, 4));

The console.log output:

[
    [
        1,
        2
    ],
    [
        3,
        4
    ],
    [
        5,
        6
    ],
    [
        7,
        8
    ],
    [
        9,
        10
    ]
]

There are tons of way you can do this. A simple one could be

 var arrayN = n => Array(n).fill([]) document.write("<pre>" + JSON.stringify(arrayN(10)) + "</pre>") 

On another thinking if you already have arrays of arrays then the most simplified way of concatenating them in place should be using the new spread operator like;

 var arr = [[1,2,3],[1,3,5]], brr = [[3,2,1],[7,8,9]]; arr.push(...brr); document.write("<pre>" + JSON.stringify(arr) + "</pre>"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM