Javascript 将数组添加到二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8184365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:00:56  来源:igfitidea点击:

Add array to two-dimensional array

javascriptmultidimensional-array

提问by dmr

Array A is a two dimensional array. It's made up of array X and Y. I'd like to add array Z to Array A as another item in Array A. How do I do this?

数组 A 是一个二维数组。它由数组 X 和 Y 组成。我想将数组 Z 添加到数组 A 作为数组 A 中的另一项。我该怎么做?

Editedto add code:

编辑添加代码:

arrayA = new Array(
    [1, 2, 3] //array x
    [4, 5, 6] //array y
    );

arrayZ = new Array(7, 8, 9);

//now, how do I add arrayZ onto the end of arrayA?

回答by Jonathan M

This will add it to the end of arrayA

这会将它添加到 arrayA 的末尾

arrayA.push(arrayZ);

Here's a reference for push: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push

这是一个参考pushhttps: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push

回答by andyb

You could push your arrays onto Array alike so

你可以a像这样将你的数组推到 Array 上

JavaScript

JavaScript

var a = new Array();
var x = new Array();
var y = new Array();
var z = new Array();

a.push(x);
a.push(y);
a.push(z);

Edit:After OP edited question with code example:

编辑:在 OP 用代码示例编辑问题之后:

var z = new Array(7, 8, 9);
var a = new Array(
    [1, 2, 3],
    [4, 5, 6]
);

a.push(z);

回答by zzzzBov

Ok, lots of responses as to how to add items to arrays, but lets start with making your code better:

好的,有很多关于如何向数组添加项的回答,但让我们从改进代码开始:

arrayA = [ //don't use new Array()
  [1, 2, 3],
  [4, 5, 6]
];

arrayZ = [7,8,9];

There're a few ways you can do this.

有几种方法可以做到这一点。

You can use the array methods unshiftor push

您可以使用数组方法unshiftpush

arrayA.unshift(arrayZ) //adds z to the front of arrayA
arrayA.push(arrayZ) //adds z to the end of arrayA

You can also set the location explicitly:

您还可以明确设置位置:

arrayA[0] = arrayZ //overwrites the first element
arrayA[1] = arrayZ //overwrites the second element
arrayA[2] = arrayZ //adds a new element at 2
arrayA[arrayA.length] = arrayZ //essentially the same as using push

You can also splicethe new element into the array:

您还可以将新元素拼接到数组中:

arrayA.splice(1, 0, arrayZ)

1specifies the start index of the elements being inserted/removed .0specifies how many elements should be removed, in this case we're adding and not removing any. arrayZis the element to insert

1指定被插入/删除的元素的起始索引。0指定应该删除多少个元素,在这种情况下,我们添加而不是删除任何元素。arrayZ是要插入的元素

回答by parapura rajkumar

Without any code I am just assuming

没有任何代码我只是假设

arr[0] = is array X

arr[0] = 是数组 X

arr[1] = is array Y

arr[1] = 是数组 Y

so you can use arr[2] for Y

所以你可以使用 arr[2] 作为 Y

var foo = new Array()
foo[0] = new Array() // Your x
foo[1] = new Array() // Your y
foo[2] = new Array() // Your z

回答by Abhishek Soni

On ES6you can use the spread operator (...) as follows:

ES6 上,您可以...按如下方式使用扩展运算符 ( ):

arrayA = [
  [1, 2, 3],
  [4, 5, 6]
];

arrayB = [7,8,9];

arrayA = [...arrayA, ...[arrayB]];