如何在JavaScript中将对象(如字符串或数字)附加到数组中?


当前回答

附加单个元素

// Append to the end
arrName.push('newName1');

// Prepend to the start
arrName.unshift('newName1');

// Insert at index 1
arrName.splice(1, 0,'newName1');
// 1: index number, 0: number of element to remove, newName1: new element


// Replace index 3 (of exists), add new element otherwise.
arrName[3] = 'newName1';

附加多个元素

// Insert from index number 1
arrName.splice(1, 0,'newElemenet1', 'newElemenet2', 'newElemenet3');
// 1: index number from where insert starts,
// 0: number of element to remove,
//newElemenet1,2,3: new elements

附加一个数组

// Join two or more arrays
arrName.concat(newAry1, newAry2);
//newAry1,newAry2: Two different arrays which are to be combined (concatenated) to an existing array

其他回答

附加单个元素

// Append to the end
arrName.push('newName1');

// Prepend to the start
arrName.unshift('newName1');

// Insert at index 1
arrName.splice(1, 0,'newName1');
// 1: index number, 0: number of element to remove, newName1: new element


// Replace index 3 (of exists), add new element otherwise.
arrName[3] = 'newName1';

附加多个元素

// Insert from index number 1
arrName.splice(1, 0,'newElemenet1', 'newElemenet2', 'newElemenet3');
// 1: index number from where insert starts,
// 0: number of element to remove,
//newElemenet1,2,3: new elements

附加一个数组

// Join two or more arrays
arrName.concat(newAry1, newAry2);
//newAry1,newAry2: Two different arrays which are to be combined (concatenated) to an existing array

附加单个项目

要将单个项附加到数组,请使用array对象提供的push()方法:

const fruits = ['banana', 'pear', 'apple']
fruits.push('mango')
console.log(fruits)

push()对原始数组进行变异。

要创建新数组,请使用concat()array方法:

const fruits = ['banana', 'pear', 'apple']
const allfruits = fruits.concat('mango')
console.log(allfruits)

请注意,concat()实际上并没有向数组中添加项,而是创建了一个新数组,您可以将其分配给另一个变量,或重新分配给原始数组(声明为let,因为无法重新分配常量):

const fruits = ['banana', 'pear', 'apple']
const allfruits = fruits.concat('mango')
console.log(allfruits)
let fruits = ['banana', 'pear', 'apple']
fruits = fruits.concat('mango')

附加多个项目

要将多个项附加到数组中,可以通过使用多个参数调用push():

const fruits = ['banana', 'pear', 'apple']
fruits.push('mango', 'melon', 'avocado')
console.log(fruits)

您还可以使用前面看到的concat()方法,传递一个用逗号分隔的项目列表:

const fruits = ['banana', 'pear', 'apple']
const allfruits = fruits.concat('mango', 'melon', 'avocado')
console.log(allfruits)

或阵列:

const fruits = ['banana', 'pear', 'apple']
const allfruits = fruits.concat(['mango', 'melon', 'avocado'])
console.log(allfruits)

请记住,如前所述,此方法不会改变原始数组,但会返回一个新数组。

最初发布于

现在大多数浏览器都支持ECMAScript 5(ES5)标准的JavaScript,您可以使用apply()将array1附加到array2。

var array1 = [3, 4, 5];
var array2 = [1, 2];

Array.prototype.push.apply(array2, array1);

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

带有ECMAScript 6(ES6)标准的JavaScript,Chrome、Firefox、Internet Explorer和Edge支持该标准,您可以使用扩展运算符:

"use strict";
let array1 = [3, 4, 5];
let array2 = [1, 2];

array2.push(...array1);

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

排列运算符将替换array2.push(…array1);带有阵列2.推(3,4,5);当浏览器思考逻辑时。

加分

如果要创建另一个变量来存储两个数组中的所有项,可以执行以下操作:

ES5 var combinedArray=array1.concat(array2);

ES6常量组合数组=[…array1,…array2]

扩展运算符(…)用于扩展集合中的所有项目。

如果要将单个值附加到数组中,只需使用push方法。它将在数组末尾添加一个新元素。

但是,如果您打算添加多个元素,请将这些元素存储在一个新数组中,并将第二个数组与第一个数组连接起来。。。无论你想怎样。

arr=['a','b','c'];
arr.push('d');
//now print the array in console.log and it will contain 'a','b','c','d' as elements.
console.log(array);

您可以使用push方法。

Array.prototype.append = function(destArray){
    destArray = destArray || [];
    this.push.call(this, ...destArray);
    return this;
}
var arr = [1,2,5,67];
var arr1 = [7,4,7,8];
console.log(arr.append(arr1)); // [7, 4, 7, 8, 1, 4, 5, 67, 7]
console.log(arr.append("Hola")) // [1, 2, 5, 67, 7, 4, 7, 8, "H", "o", "l", "a"]