How to combine first element of one array with first element of second array and first element of first array with second element of second array

huangapple go评论55阅读模式
英文:

How to combine first element of one array with first element of second array and first element of first array with second element of second array

问题

I have 2 arrays with different lengths and the number of items in each array is dynamic. I want to combine the first element of one array with the first element of the second array and the first element of the first array with the second element of the second array and so on. For example:

The first array is ['a','b','c']
The second array is ['1','2]

Finally, I need the result to look like this:

['a-1','a-2','b-1','b-2','c-1','c-2]

英文:

I have 2 arrays with different length and the number of items in each array is dynamic. I want to combine the first element of one array with first element of second array and first element of first array with second element of second array and so on.for example:

The first array is ['a','b','c']
The second array is ['1','2]

Finally, I need the result to look like this:

['a-1','a-2','b-1','b-2','c-1','c-2]

答案1

得分: 3

你可以尝试使用 flatMap() 来迭代 array1 中的每个元素,并在 array2 上使用 map()

const arr1 = ['a', 'b', 'c'];
const arr2 = ['1', '2'];

const resArray = arr1.flatMap(item1 =>
  arr2.map(item2 => `${item1}-${item2}`)
);

console.log(resArray); //["a-1","a-2","b-1", "b-2","c-1","c-2"]

你也可以使用 for循环 来实现相同的结果:

const arr1 = ['a', 'b', 'c'];
const arr2 = ['1', '2'];
const resArray = [];
for (let i = 0; i < arr1.length; i++) {
  for (let j = 0; j < arr2.length; j++) {
    const elem = arr1[i] + '-' + arr2[j];
    resArray.push(elem);
  }
}
console.log(resArray); //["a-1","a-2","b-1", "b-2","c-1","c-2"]
英文:

You can try use flatMap() to iterate over each element of array1 and use map() on array2:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr1 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];
const arr2 = [&#39;1&#39;, &#39;2&#39;];

const resArray = arr1.flatMap(item1 =&gt;
  arr2.map(item2 =&gt; `${item1}-${item2}`)
);

console.log(resArray); //[&quot;a-1&quot;,&quot;a-2&quot;,&quot;b-1&quot;, &quot;b-2&quot;,&quot;c-1&quot;,&quot;c-2&quot;]

<!-- end snippet -->

You can achieve the result using for loop as well:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr1 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];
const arr2 = [&#39;1&#39;, &#39;2&#39;];
const resArray = [];
for (let i = 0; i &lt; arr1.length; i++) {
  for (let j = 0; j &lt; arr2.length; j++) {
    const elem = arr1[i] + &#39;-&#39; + arr2[j];
    resArray.push(elem);
  }
}
console.log(resArray); //[&quot;a-1&quot;,&quot;a-2&quot;,&quot;b-1&quot;, &quot;b-2&quot;,&quot;c-1&quot;,&quot;c-2&quot;]

<!-- end snippet -->

答案2

得分: 0

你可以使用 JavaScript 中的数组方法 map flatMap 来按照描述将第一个数组的元素与第二个数组的元素组合起来。

const firstArray = ['a', 'b', 'c'];
const secondArray = ['1', '2'];

const result = firstArray.flatMap((element) => {
  return secondArray.map((value) => `${element}-${value}`);
});

console.log(result);
//输出结果: [ 'a-1', 'a-2', 'b-1', 'b-2', 'c-1', 'c-2' ]
英文:

To combine the elements of the first array with the elements of the second array as described, you can use array methods like map and flatMap in JavaScript.

const firstArray = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];
const secondArray = [&#39;1&#39;, &#39;2&#39;];

const result = firstArray.flatMap((element) =&gt; {
  return secondArray.map((value) =&gt; `${element}-${value}`);
});

console.log(result);
//Output : [ &#39;a-1&#39;, &#39;a-2&#39;, &#39;b-1&#39;, &#39;b-2&#39;, &#39;c-1&#39;, &#39;c-2&#39; ]

huangapple
  • 本文由 发表于 2023年5月25日 16:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定