简体   繁体   English

在每个'n'索引之后,将元素从一个数组插入另一个数组

[英]Insert elements from one array to another after each 'n' index

Native JS. 原生JS。 Have 2 arrays, for example 例如,有2个数组

arr1 = ['orange', 'blue', 'red', 'black', 'white', 'magenta', 'cyan']
arr2 = [1, 2, 3]

I need a function, that combine this 2 arrays, like this 我需要一个函数,将这两个数组结合起来,像这样

function arrInsertAfter(arr1, arr2, afterElement) 
// for example make afterElement = 2 - insert element of second array after each 2 elements of first array

....

And result should be 结果应该是

['orange', 'blue', 1, 'red', 'black', 2, 'white', 'magenta', 3, 'cyan']

If afterElement = 3 for example it should return 例如,如果afterElement = 3 ,则应返回

['orange', 'blue', 'red', 1, 'black', 'white', 'magenta', 2, 'cyan', 3] // append remaining elements of second array at the end simply

It's important to not use any third-party connected lib's. 不要使用任何第三方连接的库,这一点很重要。

Here is my function 这是我的功能

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;

    }

With traditional loop 与传统循环

  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);

This should do :) 这应该做:)

 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); 

Using forEach on an array you are mutating in the callback function can be tricky to do. 在数组中使用forEach在回调函数中进行变异可能很棘手。 Here's a mutating approach using traditional loops and splice to add elements into the a array at specific index locations: 下面是使用传统的循环和不同诱变方法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); 

Here's a non-mutating version that preserves both parameters: 这是保留两个参数的非变异版本:

 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)); 

And if you don't mind mutating b : 而且,如果您不介意将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)); 

You could take a copy and splice the elements at the right position. 您可以复制并在正确位置拼接元素。

 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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在给定索引下将一个不变列表中的所有元素插入到另一个中 - How to insert all elements from one immutable list into another at given index 如何比较一个数组中的元素与Javascript中另一个数组的索引? - how to compare the elements of one array to the index of another array in Javascript? 在每个 nt-h 数组元素后插入新元素 - Insert new element after each nt-h array elements 给定JavaScript中的特定索引,如何将元素从一个数组复制到另一种不同类型的数组? - How can I copy elements from one array to another of a different type given a particular index in JavaScript? 将数据从JavaScript数组插入另一个数组 - Insert datas from a JavaScript array to another one 如果元素本身在另一个数组中,如何在特定索引的数组中插入元素? - How to insert elements in an array at a particular index in case the elements are themselves in another array? 在 JavaScript 中从另一个数组中获取一个数组的元素 - Get elements of one array from another in JavaScript 将元素从一个数组添加到另一个数组 - Adding elements from one array to another 将一个数组中的每个值与另一个数组中的每个值组合 - Combine each of the values from one array with each of the values in another array 从多维数组的每个索引中删除最后 2 个元素 - Remove the last 2 elements from each index in a Multidimensional Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM