简体   繁体   中英

JS Object Loop returns undefined, whereas Lodash map returns value

I have a loop in JS, and once the loop is complete the value is returning as null, if I use lodash for the same purpose, it returns the result.

This is what I tried:

JS:

const jsRows = Object.entries(response).forEach(([key, row]) => {
    return row;
});
console.log(jsRows) ==> Output undefined

Lodash:

const loRows = map(response, row => {
    return row;
});
console.log(loRows) ==> Output Array

The .forEach() function always returns undefined . It is not at all the same thing as a .map() function. The native JavaScript Array.prototype.map() also returns a new array.

The .forEach() function is for "doing something" with each element of an array in a situation where you don't need a new result comprised of a transformation of each array value. The .map() function, on the other hand, is for transforming an array into a new array based on a transformation of each array element.

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