简体   繁体   English

在JavaScript中动态添加数组数组

[英]Adding array of arrays dynamically in javascript

I am trying to add array of arrays dynamically in javascript. 我试图在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. 我尝试使用concat()和merge()方法,这些方法将内容直接添加到单个数组中,但这不是我想要的。 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 " 您已经写道“我正在尝试在javascript中动态添加数组数组”
The simple way is using Array.prototype.push method: 简单的方法是使用Array.prototype.push方法:

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: console.log输出:

[
    [
        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>"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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