简体   繁体   English

如何连接两个 arrays 槽循环,Javascript?

[英]How to concatenate two arrays trough loop, Javascript?

Let's say that I have this array.假设我有这个数组。

const arr = [['grass', 'water'], ['fire', 'ground'], ['fairy', 'mage'], ['fighter', 'fire']];

So what I want is to concatenate arr[0] with arr[2], arr[1] with arr[3].所以我想要的是连接 arr[0] 和 arr[2],arr[1] 和 arr[3]。

This has to be done trough some array method or loop, since I don't know how much of elements I will have.这必须通过一些数组方法或循环来完成,因为我不知道我将拥有多少元素。

Result should look like this结果应该是这样的

const arr = [['grass', 'water', 'fairy', 'mage'], ['fire', 'ground', 'fighter', 'fire']];

If you mod the current index with half the length of the array, then you would get corresponding index in the resultant array.如果您使用数组长度的一半修改当前索引,那么您将在结果数组中获得相应的索引。

Index in original array原始数组中的索引

0 1 2 3 4 5

Index in resultant array结果数组中的索引

0 1 2 0 1 2  // After taking modulus with 3 i.e. half of the array length

Solution解决方案

  1. I've used the Nullish coalescing operator to assign the res array with empty array if the desired position is empty.如果所需的 position 为空,我使用Nullish coalescing operatorres数组分配空数组。

  2. Then push all the elements into this position and finally return the resultant.然后将所有元素推入这个 position 并最终返回结果。

NOTE: This solution only works for arrays of even length.注意:此解决方案仅适用于偶数长度的 arrays。

 const arr = [ ["grass", "water"], ["fire", "ground"], ["fairy", "mage"], ["fighter", "fire"], ["water", "ice"], ["ground", "rock"], ]; const result = arr.reduce( (r, el, i) => ( (r[i % (arr.length / 2)]??= []), r[i % (arr.length / 2)].push(...el), r ), [] ); console.log(result);

You can do something like this:你可以这样做:

 const arr = [['grass', 'water'], ['fire', 'ground'], ['fairy', 'mage'], ['fighter', 'fire']]; const newArr = []; for (let i = 0; i < arr.length/2; i++){ newArr.push(arr[i].concat(arr[i + 2])); } console.log(newArr);

Here are few options that work for even numbered arrays.以下是一些适用于偶数 arrays 的选项。

No mutations无突变

  1. Iterate from the start to half the array.从开始迭代到数组的一半。

  2. Join the item at the current index with the one that is equal distance from the middle of the array:将当前索引处的项目与与数组中间距离相等的项目连接起来:

    • Iteration 1:迭代 1:
     [0, 1, 2, 3] ^ ^
    • Iteration 2:迭代 2:
     [0, 1, 2, 3] ^ ^
  3. Add each of these to a new array.将这些中的每一个添加到新数组中。 The original and all its members are untouched.原版及其所有成员均未受影响。

With a loop有一个循环

 function* slice(start, end, arr) { if (start < 0) start = arr.length + start; if (end < 0) end = arr.length + start; for (let i = start; i < end; i++) { yield arr[i]; } } const arr = [['grass', 'water'], ['fire', 'ground'], ['fairy', 'mage'], ['fighter', 'fire']]; const middle = arr.length / 2; const result = Array.from( slice(0, middle, arr), (first, i) => first.concat(arr[middle + i]) ); console.log(result);

With array methods使用数组方法

 const arr = [['grass', 'water'], ['fire', 'ground'], ['fairy', 'mage'], ['fighter', 'fire']]; const middle = arr.length / 2; const result = arr.slice(0, middle).map((first, i) => first.concat(arr[middle + i])); console.log(result);

Using a generator使用生成器

To avoid intermediate arrays from .slice() you can use a generator function and pass it through Array.from() supplying a mapping function to generate an array:为了避免来自.slice()的中间 arrays 您可以使用生成器 function并将其传递给Array.from()提供映射 ZC1C425268E68385D1AB5074C17A94F14 以生成数组:

 const arr = [['grass', 'water'], ['fire', 'ground'], ['fairy', 'mage'], ['fighter', 'fire']]; const middle = arr.length / 2; const result = arr.slice(0, middle).map((first, i) => first.concat(arr[middle + i])); console.log(result);

With mutations有突变

  1. Start before the middle of the array and go backwards.在阵列中间之前开始,go 向后。

  2. Remove the last item in the array.删除数组中的最后一项。 Add its members to the item at the current index.将其成员添加到当前索引处的项目。

    • Iteration 1:迭代 1:
     [0, 1, 2, 3] ^ ^ remove last: 3 combine with: 1
    • Iteration 2:迭代 2:
     [0, 13, 2] ^ ^ remove last: 2 combine with: 0
    • End result:最终结果:
     [02, 13]
  3. This is all done in-place.这一切都是就地完成的。 Both arr and its members are modified. arr及其成员都被修改了。

 const arr = [['grass', 'water'], ['fire', 'ground'], ['fairy', 'mage'], ['fighter', 'fire']]; const middle = arr.length / 2; for (let i = middle - 1; i >= 0; i--) { const first = arr[i]; const second = arr.pop(); first.push(...second); } console.log(arr);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM