简体   繁体   English

javascript数组取消移位某些元素

[英]javascript array unshift some elements

I have two arrays. 我有两个数组。 The first array contains some values while the second array contains some elements which should be inserted to the first array. 第一个数组包含一些值,而第二个数组包含一些应插入第一个数组的元素。 For example: 例如:

var aArr = [ {'a':111}, {'b':222}, {'c':333} ... ]
var bArr = [ {'x': 0}, {'y': 0} ] // I'm not sure it's length 

// finail I need got something like 
var aArr = [{'x': 0}, {'y': 0}, {'c':333} ...]

and bArr array is not sure it's length ,maybe it has one element 并且bArr数组不确定其长度,也许它只有一个元素

var aArr = [ {'a':111}, {'b':222}, {'c':333} ... ]
var bArr = [ {'x': 0} ] 

// finail I need got something like 
var aArr = [{'x': 0}, {'b': 222}, {'c':333} ...]

I use splice() which not work very well 我使用的splice()效果不好

var a = [1, 2, 3],
    b = [4, 5];

function prependArray(a, b) {

    a.splice(0, b.length, b);
    return a;
}

var result = prependArray(a, b);
// result =  [[4, 5], 3]  not  [ 4, 5, 3]

What should I do? 我该怎么办? ? I need more efficient method, because the aArr have more than 3000 values, and bArr have more than 100 values. 我需要更有效的方法,因为aArr具有超过3000个值,而bArr具有超过100个值。

thanks. 谢谢。

There are many ways to do this, but not as many that preserve the original aArr reference (eg modify the actual aArr array without creating a new one). 有很多方法可以做到这一点,但是保留原始aArr引用的方法aArr (例如,修改实际的aArr数组而不创建新的aArr数组)。 Here's one way: 这是一种方法:

aArr.splice(0, bArr.length);            // remove front-most bArr.length items from aArr
aArr.unshift.apply(aArr, bArr);         // insert bArr items in front of aArr

This removes the first bArr.length items form aArr and then adds the bArr items to the front of aArr , all the while preserving the original aArr reference (eg not replacing it with a new array). 这消除了第一bArr.length项形式aArr ,然后添加bArr项的前aArr ,而同时保持原始aArr参考(例如,不与新的阵列替换它)。


It can also be done in one .splice() , but that requires building a temporary array to pass to .splice.apply() which didn't seem worth it since that makes an entirely new array just to save one line of code. 它也可以在一个.splice() ,但是这需要构建一个临时数组以传递给.splice.apply() ,这似乎不值得,因为这使得一个全新的数组仅用于保存一行代码。 In any case, that would look like this: 无论如何,这看起来像这样:

aArr.splice.apply(aArr, [0, bArr.length].concat(bArr));

If you really want max "efficiency" in terms of performance rather than in terms of lines of code, then you will probably need to do performance benchmarks using a tool like jsperf and test in multiple browsers. 如果您真的想在性能方面而不是在代码行方面获得最大的“效率”,那么您可能需要使用jsperf之类的工具进行性能基准测试并在多个浏览器中进行测试。 It may be that simply copying over the bArr items into aArr is the most "efficient" because that has the fewest array manipulations. 可能仅将bArr项目复制到aArr中是最“有效的”,因为这样做的阵列操作最少。 To know for sure, you would have to measure actual performance at your typical array sizes across several browsers. 要确定地知道,您将必须在几种浏览器中以典型阵列大小来衡量实际性能。

For pure performance, you should test this vs the options above: 为了获得纯性能,您应该对比上面的选项进行测试:

for (var i = 0, len = bArr.length; i < len; i++) {
    aArr[i] = bArr[i];
}

This has the advantage that it does not create any temporary arrays and does not have to shift the items in aArr around. 这样做的好处是,它不会创建任何临时数组,也不aArr的项目四处移动。 It has the disadvantage of running the loop in plain javascript, not in native array manipulation code. 它的缺点是无法使用纯JavaScript而非本地数组操作代码运行循环。


It appears that the last option of just copying elements over is 7x faster in Chrome, 10x faster in IE11 and even more of a difference in Firefox. 看来,仅复制元素的最后一个选择是在Chrome中快7倍,在IE11中快10倍,在Firefox中差异更大。

See the jsperf here: http://jsperf.com/array-shift-vs-copy 在此处查看jsperf: http ://jsperf.com/array-shift-vs-copy

在此处输入图片说明

function prependArray(a, b) {
    return a.splice.apply(a, [0, b.length].concat(b))
}

Thanks The Paramagnetic Croissant (*_*) 谢谢顺磁牛角包(* _ *)

There is a concat-Method in Javascript: Javascript中有一个concat-Method:

result = b.concat(a);

Edit: forgot to remove the old values in a: 编辑:忘记删除一个中的旧值:

After the concat, apply splice: 合并后,进行拼接:

result.splice(b.length, b.length);

Working example: http://jsfiddle.net/9sLjnu66/ 工作示例: http : //jsfiddle.net/9sLjnu66/

 var aArr = [{ 'a': 111 }, { 'b': 222 }, { 'c': 333 }] var bArr = [{ 'x': 0 }, { 'y': 0 }] // I'm not sure it's length for (var i = 0; i < bArr.length; i++) { if (i > aArr.length) break; aArr[i] = bArr[i]; } //show the results document.write(JSON.stringify(aArr)); 
 The result should be: <br> <code> [{'x': 0}, {'y': 0}, {'c':333}] </code> <br>And it is: <br> 

在ECMAScript 6中:

a.splice(0, b.length, ...b);

Another idea is 另一个想法是

bArr.concat(aArr.slice(bArr.length))

In other words, slice off the first n elements of the first array, where n is the length of the second array, then append that to the second array. 换句话说,将第一个数组的前n个元素(其中n是第二个数组的长度)切开,然后将其附加到第二个数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM