简体   繁体   中英

how to understand below function output for node js

I have below function which take result set which has rows(first row is heading) and columns (like dataset in .NET) as input and return output. What output it returns I am not able to understand. I am facing some issues to run Node js app and hence not able to debug it line by line. I really need to understand its purpose.

export const flattenObject = (object) => {
  return object.reduce((acc, val) => Array.isArray(val) ?
    acc.concat(flattenObject(val)) : acc.concat(val), []);
};

Could someone please help me to get its output/purpose in code. Thanks in advance.

The arrow function will be executed on every item in the array, acc will be the item that was returned from the previous execution and val will be the item itself. In the first execution, acc will be the second param, in this case, an empty array.

So what it does is checks if the current item in the array is an array, and if so, recursively runs the function, and if it's not, is adds the item to the array.

This flattens the array. If object is [[1,[2,3]],4] it will return [1,2,3,4] .

Better way to do it is using object.flat(Infinity) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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