如何用另一个数组的元素替换数组中的元素
How to replace elements in array with elements of another array
我想用另一个可变长度数组的元素替换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
是一个非常大的数组,则不鼓励使用此方法,因为...b
在splice
的参数中被扩展,并且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]
- 在函数中添加数组元素的数值
- 访问JSON对象内部的数组元素
- 用于检查数组中是否存在元素的javascript自定义方法
- 在数组中的一个元素上设置多个值
- 从组件状态的数组中删除元素
- Mongoose-在更新中删除数组元素
- 从数组中删除元素的最佳方法是:javascript/jquery
- 数组中元素(字符串)的总和
- javascript数组元素是否知道其封闭数组
- 查找数组javascript中包含的元素类型
- 如何在javascript上向数组的对象添加新元素
- 如果数组仍在执行,async.forEach()将运行我添加到数组中的新元素
- 使用filter和map方法将数组中某些元素的第一个字母大写-JavaScript
- 一些元素没有从数组中删除
- JS.循环遍历多维数组,以计数元素在每列中的出现次数
- 将数组元素附加到FormData dos'不适用于Firefox 15
- 元素不会添加到Javascript数组中
- 如何从另一个带下划线的数组中筛选带元素的数组
- 如何将函数应用于两个数组的第 n 个元素(数组的数组)
- Js接受两个值之间的元素(数组)