开发者

Javascript Arrays merging using indexes

I'm trying to merge 2 javascript arrays using indexes.

let's say I have array A with

A[0] = 1;
A[1] = 9;
...
A[5] = 12;

and array B开发者_如何学编程 with:

B[0 ... 5] = garbage, unused;
B[6] = 23;
B[7] = 99;
B[8] = 31;
...
B[10] = 990;

I want to merge A with B to do:

merged[0] = 1;
merged[1] = 9;
..
merged[5] = 12;
merged[6] = 23;
...
merged[10] = 999;

How can this be done?


Use a combination of concat and slice:

var C = A.concat(B.slice(6));

concat joins together two arrays and returns the result, while slice creates a copy of a portion of an array, where you specify the starting [and ending] index[es] of that portion.

http://jsfiddle.net/xaERK/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜