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


当前回答

有两种方法可以在JavaScript中附加数组:

1) push()方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。

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

输出:

[1, 2, 3, 4, 5]

2) unshift()方法将一个或多个元素添加到数组的开头,并返回数组的新长度:

var a = [1, 2, 3];
a.unshift(4, 5);
console.log(a); 

输出:

[4, 5, 1, 2, 3]

3) concat()方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

var arr1 = ["a", "b", "c"];
var arr2 = ["d", "e", "f"];
var arr3 = arr1.concat(arr2);
console.log(arr3);

输出:

[ "a", "b", "c", "d", "e", "f" ]

4) 可以使用数组的.length属性将元素添加到数组的末尾:

var ar = ['one', 'two', 'three'];
ar[ar.length] = 'four';
console.log( ar ); 

输出:

 ["one", "two", "three", "four"]

5) splice()方法通过删除现有元素和/或添加新元素来更改数组的内容:

var myFish = ["angel", "clown", "mandarin", "surgeon"];
myFish.splice(4, 0, "nemo");
//array.splice(start, deleteCount, item1, item2, ...)
console.log(myFish);

输出:

["angel", "clown", "mandarin", "surgeon","nemo"]

6) 您还可以通过指定新索引并赋值来向数组中添加新元素:

var ar = ['one', 'two', 'three'];
ar[3] = 'four'; // add new element to ar
console.log(ar);

输出:

["one", "two","three","four"]

其他回答

使用Array.prototype.push方法将值附加到数组末尾:

//初始化数组变量arr=[“嗨”,“你好”,“你好”];//将新值附加到数组arr.push(“Hola”);控制台日志(arr);


您可以使用push()函数在一次调用中将多个值附加到数组中:

//初始化数组var arr=[“Hi”,“你好”,“Bonjour”,“Hola”];//将多个值附加到数组arr.push(“Salut”,“嘿”);//显示所有值对于(变量i=0;i<arr.length;i++){控制台日志(arr[i]);}


使现代化

如果要将一个数组的项添加到另一个数组,可以使用firstArray.concat(secondArray):

变量arr=[“苹果”,“香蕉”,“樱桃”];//不要忘记分配结果,因为与push不同,concat不会更改现有数组arr=arr.concat([“龙果”,“接骨木”,“无花果”]);控制台日志(arr);

使现代化

如果你想在数组的开头加上任何值(即第一个索引),那么你可以使用array.prototype.unshift。

var arr=[1,2,3];arr.unshift(0);控制台日志(arr);

它还支持像push一样一次附加多个值。


使现代化

ES6语法的另一种方法是使用扩展语法返回新数组。这使原始数组保持不变,但返回一个附加了新项的新数组,符合函数式编程的精神。

常量arr=[“嗨”,“你好”,“你好”,];常量newArr=[…arr,“致敬”,];console.log(newArr);

我认为值得一提的是,push可以用多个参数调用,这些参数将按顺序附加到数组中。例如:

var arr=['first'];arr.push('第二个','第三个');控制台日志(arr);

因此,您可以使用push.apply将一个数组附加到另一个数组,如下所示:

var arr=['first'];arr.push('第二个','第三个');arr.push.apply(arr,['forth','fifth']);控制台日志(arr);

带注释的ES5提供了更多关于推送和应用功能的信息。

2016年更新:使用spread,您不再需要应用,例如:

var arr=['first'];arr.push('第二个','第三个');arr.push(…['furth','fifth']);控制台日志(arr);

附加单个元素

// 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

您可以使用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"]

我们在JavaScript中没有Array的append函数,但我们有push和unshift。假设您有以下阵列:

var arr = [1, 2, 3, 4, 5];

我们喜欢向这个数组附加一个值。我们可以执行arr.push(6),它将在数组末尾添加6:

arr.push(6); // Returns [1, 2, 3, 4, 5, 6];

我们还可以使用unshift,看看我们如何应用它:

arr.unshift(0); // Returns [0, 1, 2, 3, 4, 5];

它们是向数组添加或追加新值的主要函数。