本地JS。 例如有2个数组
arr1 = ['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan']
arr2 = [1, 2, 3]
我需要一个将这2个数组组合在一起的函数,像这样
function arrInsertAfter(arr1, arr2, afterElement)
// for example make afterElement = 2 - insert element of second array after each 2 elements of first array
....
结果应该是
['orange', 'blue', 1, 'red', 'black', 2, 'white', 'magenta', 3, 'cyan']
例如,如果afterElement = 3
应该返回
['orange', 'blue', 'red', 1, 'black', 'white', 'magenta', 2, 'cyan', 3] // append remaining elements of second array at the end simply
重要的是不要使用任何第三方连接的库。
这是我的职能
let arr1 = ['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan'],
arr2 = [1, 2, 3];
function arrInsertAfter(arr1, arr2, afterElement) {
let curPos = afterElement;
arr2.forEach(function(e) {
arr1.splice(curPos, 0, e);
curPos = afterElement+curPos+1;
});
return arr1;
}
使用传统循环
function arrInsertAfter(arr1, arr2, afterElement) {
let curPos = afterElement;
for (let i = 0; i < arr2.length; i++) {
arr1.splice(curPos, 0, arr2[i]);
curPos = (curPos+afterElement)+1;
}
return true;
}
arrInsertAfter(arr1, arr2, 2);
alert(arr1);
答案 0 :(得分:2)
这应该做:)
function arrInsertAfter(arr1, arr2, afterElement){
result = [];
for(var i = 1; arr2.length>0 || arr1.length>0;i++){
if(arr1.length>0){
result.push(arr1.shift());
}
if(i%afterElement==0 && arr2.length>0){
result.push(arr2.shift());
}
}
return result;
}
const arr1 = ['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan'];
const arr2 = [1, 2, 3];
result = arrInsertAfter(arr1, arr2, 3);
console.log(result);
答案 1 :(得分:1)
在要在回调函数中进行突变的数组上使用forEach
可能很棘手。这是一种使用传统循环和splice
在特定索引位置的a
数组中添加元素的变异方法:
function arrInsertAfter(a, b, after) {
let j = 0;
for (let i = after; i < a.length && j < b.length; i += after) {
a.splice(i++, 0, b[j++]);
}
while (j < b.length) {
a.push(b[j++]);
}
}
const arr1 = ['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan'];
const arr2 = [1, 2, 3];
arrInsertAfter(arr1, arr2, 3);
console.log(arr1);
这是一个保留两个参数的非突变版本:
function arrInsertAfter(a, b, after) {
const res = a.slice(0);
let j = 0;
for (let i = after; i < res.length && j < b.length; i += after) {
res.splice(i++, 0, b[j++]);
}
while (j < b.length) {
res.push(b[j++]);
}
return res;
}
const arr1 = ['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan'];
const arr2 = [1, 2, 3];
console.log(arrInsertAfter(arr1, arr2, 2));
console.log(arrInsertAfter(arr1, arr2, 3));
如果您不介意更改b
:
const arrInsertAfter = (a, b, after) =>
a.reduce((r, e, i) =>
r.concat(i && i % after === 0 && b.length ? [b.shift(), e] : [e])
, []).concat(b)
;
const arr1 = ['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan'];
const arr2 = [1, 2, 3];
console.log(arrInsertAfter(arr1, arr2.slice(), 2));
console.log(arrInsertAfter(arr1, arr2, 3));
答案 2 :(得分:1)
您可以复制并在正确位置拼接元素。
function insertAfter(array1, array2, n) {
var temp = array1.slice();
array2.forEach((v, i) => temp.splice((i + 1) * n + i, 0, v));
return temp;
}
console.log(insertAfter(['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan'], [1, 2, 3], 2));
console.log(insertAfter(['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan'], [1, 2, 3], 3));
.as-console-wrapper { max-height: 100% !important; top: 0; }