[英]Javascript: How to use the reduce function and the concat array function to transform an array of arrays into an array?
Javascript: How to use the reduce function and the concat array function to transform an array of arrays into an array? Javascript:如何使用reduce function 和concat 数组function 将数组ZA3CBC549D0CE2D71C 转换为一个数组?
In the following code, the array of arrays is not transformed and the browser console log logs an empty array.在以下代码中,未转换 arrays 数组,浏览器控制台日志记录了一个空数组。
Under the code below, what is the exact reason for the failure?在下面的代码下,失败的确切原因是什么?
Any help would be greatly appreciated.任何帮助将不胜感激。
// use reduce to transform an array of
// arrays to one array that combines all.
// elements.
function combine(array1d, element)
{
array1d.concat(element);
return array1d;
}
function reduce(array2d, combine) {
let array1d = [];
for (let element of array2d) {
array1d = combine(array1d, element);
}
return array1d;
}
let array2d = [[1, 2,],[3, 4]];
console.log(reduce(array2d, combine));
You can transform the 2D array into an array first with flat
, and then use the reduce
function to add the elements.您可以先使用flat
将二维数组转换为数组,然后使用reduce
function 添加元素。
If you don't need to add the elements, just remove the reduce
.如果您不需要添加元素,只需删除reduce
。
var res = array2d.flat().reduce((prev, curr) => prev + curr);
Ciao, here working example: Ciao,这里的工作示例:
let array2d = [[1, 2,],[3, 4]]; let result = array2d.flat(1); console.log(result);
const res = array2d.reduce((arr, seq) => {
arr.push(...seq);
return arr;
}, [])
console.log(res)
As you can see by all the answers, there are multiple ways to do this.正如您从所有答案中看到的那样,有多种方法可以做到这一点。
function combine(array1d, element) {
return array1d.concat(element);
}
function reduce(array2d, reducerFn) {
let array1d = [];
for (let element of array2d) {
array1d = reducerFn(array1d, element);
}
return array1d;
}
let array2d = [[1, 2,],[3, 4]];
console.log(reduce(array2d, combine));
The difference here is the return array1d.concat(element);
这里的区别在于return array1d.concat(element);
call instead of returning array1d
and call .concat
on it.调用而不是返回array1d
并调用.concat
。 concat is side-effect free, so it creates a new array instead of mutating the original one. concat没有副作用,因此它创建一个新数组而不是改变原始数组。 I've also renamed combine
in reduce
to reducerFn
to make it more clear that it could be changed to something else.我还将reduce
中的combine
重命名为reducerFn
以更清楚地表明它可以更改为其他内容。
let result = array2d.reduce(function (flattenedArray, element) {
return flattenedArray.concat(element);
}, []);
reduce
and the spread operator instead of concat
(no IE support - if you can drop it, you can also use Arrow functions ):使用reduce
和扩展运算符而不是concat
(不支持 IE - 如果可以删除它,也可以使用箭头函数):let result = array2d.reduce(function (flattenedArray, element) {
return [...flattenedArray, ...element];
}, []);
let result = array2d.flatMap(element => element);
let result = array2d.flat();
flatMap
and reduce
, like concat
, also return a new array, without mutating the original one. flatMap
和reduce
与concat
一样,也返回一个新数组,而不会改变原始数组。 Since the original version looked more like trying to use a functional programming style to me, I guess these are a good addition to your toolbox.由于原始版本在我看来更像是尝试使用函数式编程风格,我想这些是您工具箱的一个很好的补充。 flatMap
and flat
are relatively recent additions to the language, so depending on the environments you need to support, you might end up using something else instead. flatMap
和flat
是该语言相对较新的添加,因此根据您需要支持的环境,您最终可能会改用其他东西。
Why don't you do it the old fashioned way?你为什么不按照老式的方式来做呢?
function mergeArray(array){
let finalArray= []
for(let i=0;i<iarray.lenght;i++){
for(let j=0;j<array[i].lenght;j++){
finalArray.push(array[i][j])
}
}
return finalArray
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.