英文:
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 = ['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"]
<!-- 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 = ['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"]
<!-- 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 = ['a', 'b', 'c'];
const secondArray = ['1', '2'];
const result = firstArray.flatMap((element) => {
return secondArray.map((value) => `${element}-${value}`);
});
console.log(result);
//Output : [ 'a-1', 'a-2', 'b-1', 'b-2', 'c-1', 'c-2' ]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论