如何用另一个数组的元素替换数组中的元素

How to replace elements in array with elements of another array

本文关键字:元素 数组 替换 何用 另一个      更新时间:2023-09-26

我想用另一个可变长度数组的元素替换0元素中的某个数组中的元素。类似:

var arr = new Array(10), anotherArr = [1, 2, 3], result;
result = anotherArr.concat(arr);
result.splice(10, anotherArr.length);

有更好的方法吗?

您可以使用splice方法用另一个数组中的项替换数组的一部分,但您必须以特殊的方式调用它,因为它希望项作为参数,而不是数组。

splice方法需要像(0, anotherArr.Length, 1, 2, 3)这样的参数,所以您需要创建一个带有参数的数组,并使用apply方法调用带有参数的splice方法:

Array.prototype.splice.apply(arr, [0, anotherArr.length].concat(anotherArr));

示例:

var arr = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
var anotherArr = [ 1, 2, 3 ];
Array.prototype.splice.apply(arr, [0, anotherArr.length].concat(anotherArr));
console.log(arr);

输出:

[ 1, 2, 3, 'd', 'e', 'f', 'g', 'h', 'i', 'j']

演示:http://jsfiddle.net/Guffa/bB7Ey/

在带有单个操作的ES6中,可以用b:的元素替换a的第一个b.length元素

let a = [1,  2,  3,  4,  5]
let b = [10, 20, 30]
a.splice(0, b.length, ...b)
console.log(a) // -> [10, 20, 30, 4, 5]

使用拼接长度中的a.length(或Infinity替换数组的全部内容也很有用:

let a = [1,  2,  3,  4,  5]
let b = [10, 20, 30]
a.splice(0, a.length, ...b)
// or
// a.splice(0, Infinity, ...b)
console.log(a) // -> [10, 20, 30], which is the content of b

a数组的内容将被b内容完全替换。

注意1:在我看来,数组突变应该只用于性能关键的应用程序,如高FPS动画,以避免创建新的数组。通常我会创建一个新的数组来保持不变性。

注意2:如果b是一个非常大的数组,则不鼓励使用此方法,因为...bsplice的参数中被扩展,并且JS函数可以接受的参数数量有限制。在这种情况下,我鼓励使用另一种方法(或者创建一个新的数组,如果可能的话!)。

在ES6、TypeScript、Babel或类似软件中,您可以执行以下操作:

arr1.length = 0; // Clear your array
arr1.push(...arr2); // Push the second array using the spread opperator

很简单。

对于任何想用另一个数组的全部内容替换一个数组中的全部内容同时保留原始数组的人:

Array.prototype.replaceContents = function (array2) {
    //make a clone of the 2nd array to avoid any referential weirdness
    var newContent = array2.slice(0);
    //empty the array
    this.length = 0;
    //push in the 2nd array
    this.push.apply(this, newContent);
};

prototype函数将一个数组作为参数,作为新的数组内容,对其进行克隆以避免任何奇怪的引用,清空原始数组,然后将传入的数组作为内容推送。这将保留原始数组和任何引用。

现在你可以简单地这样做:

var arr1 = [1, 2, 3];
var arr2 = [3, 4, 5];
arr1.replaceContents(arr2);

我知道这并不是最初的问题,但当你在谷歌上搜索时,这个问题会首先出现,我想其他人可能会觉得这很有帮助,因为这是我需要的答案。

您可以只使用splice,可以在删除旧元素的同时添加新元素:

var arr = new Array(10), anotherArr = [1, 2, 3];
arr.splice.apply(arr, [0, anotherArr.length].concat(anotherArr))

如果您不想修改arr数组,可以使用slice返回数组的浅拷贝:

var arr = new Array(10), anotherArr = [1, 2, 3], result = arr.slice(0);
result.splice.apply(result, [0, anotherArr.length].concat(anotherArr));

或者,您可以使用slice来剪切第一个元素,并在top:上添加anotherArr

result = anotherArr.concat(arr.slice(anotherArr.length));

我不确定这是否是一种"更好"的方式,但至少它允许您选择起始索引(而您的解决方案只能从索引0开始工作)。这是一把小提琴。

// Clone the original array
var result = arr.slice(0);
// If original array is no longer needed, you can do with:
// var result = arr;
// Remove (anotherArr.length) elements starting from index 0
// and insert the elements from anotherArr into it
Array.prototype.splice.apply(result, [0, anotherArr.length].concat(anotherArr));

(该死,这么多忍者。:-p)

在这种情况下,您只需设置数组的长度。对于更复杂的案例,请参阅@Guffa的回答。

var a = [1,2,3];
a.length = 10;
a; // [1, 2, 3, undefined x 7]