JS 合并数组
案例说明:
某充话费充值渠道支持a充值列表,但某充值卡的面额只有b列表面额
其中other表示其他输入金额
var a = [5, 10, 20, 50, 100, 'other'];
var b = [10, 30, 50];
理想结果:a渠道能支持b卡充值:[10, 50]
function ArrayIntersection( a, b ) {//合并数组
var ai=0, bi=0;
var result = new Array();
while ( ai < a.length && bi < b.length ) {
if ( a[ai] < b[bi] ) {
ai++;
} else if ( a[ai] > b[bi] ) {
bi++;
} else {
result.push ( a[ai] );
ai++;
bi++;
}
}
return result;
}
var new_item = ArrayIntersection(a, b); console.log(new_item );
[10, 50]
相关推荐
Zhongmeishijue 2020-09-10
runner 2020-09-01
梦的天空 2020-08-25
IdeaElements 2020-08-19
luvhl 2020-08-17
移动开发与培训 2020-08-16
ReunionIsland 2020-08-16
lyqdanang 2020-08-16
NARUTOLUOLUO 2020-08-03
MyNameIsXiaoLai 2020-07-08
星辰的笔记 2020-07-04
csstpeixun 2020-06-28
letheashura 2020-06-26
liaoxuewu 2020-06-26
OldBowl 2020-06-26
北京老苏 2020-06-25
Luffyying 2020-06-25