admin管理员组文章数量:1287891
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?
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?
Share Improve this question asked Jul 16, 2011 at 11:58 Nick Fr.Nick Fr. 511 silver badge2 bronze badges1 Answer
Reset to default 10Use a bination 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/xaERK/
本文标签: Javascript Arrays merging using indexesStack Overflow
版权声明:本文标题:Javascript Arrays merging using indexes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282251a2370073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论