数组合并
一:合并后的数组为:['A1','A2','A',B1','B2','B','C1','C2','C','D1','D2','D']
let ary1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let ary2 = ['A', 'B', 'C', 'D'];
ary2 = ary2.map(item => item + '3');
let arr = ary1.concat(ary2);
arr = arr.sort((a, b) => a.localeCompare(b)).map(item => {
return item.replace('3', '');
})
二:合并后的数组为:['D1','D2','D','A1','A2','A','C1','C2','C','B1','B2','B']
let ary1 = ['D1', 'D2', 'A1', 'A2', 'C1', 'C2', 'B1', 'B2'];
let ary2 = ['B', 'A', 'D', 'C'];
let n = 0;
for (let i = 0; i < ary2.length; i++) {
let item2 = ary2[i];
for (let k = 0; k < ary1.length; k++) {
let item1 = ary1[k];
if (item1.includes(item2)) {
// => 如果包含就记录一下当前这一项的索引位置(后面还有包含的会重新记录这个值)
n = k;
}
}
// => 把当前 item2 这一项插入到 ary1 中 n 的后面
ary1.splice(n + 1, 0, item2);
}
console.log(ary1)