javascript将数组添加到数组中的特定索引

时间:2018-07-19 15:51:56

标签: javascript

我想问如何将数组添加到另一个数组的末尾,但是新添加的 数组在一个索引中 例如,如果我想添加

fruits=["banana","grape"]

fruits=["banana","grape",["green","red","yellow"]]

我希望结果像

["banana","grape","green","red","yellow"]

而不是

typeValidator

1 个答案:

答案 0 :(得分:1)

如果您想始终将数组添加到数组的末尾,则可以像这样使用.push()

let apples = ["green","red","yellow"];
let fruits = ["banana","grape"];
fruits.push(apples); // appends the array apples to the end of fruits
console.log(fruits);