简体   繁体   English

JS对象循环返回未定义,而Lodash映射返回值

[英]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. 我在JS中有一个循环,一旦循环完成,该值将返回为null,如果出于相同目的使用lodash,它将返回结果。

This is what I tried: 这是我尝试的:

JS: JS:

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

Lodash: Lodash:

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

The .forEach() function always returns undefined . .forEach()函数始终返回undefined It is not at all the same thing as a .map() function. 它与.map()函数完全不同。 The native JavaScript Array.prototype.map() also returns a new array. 本机JavaScript Array.prototype.map()也返回一个新数组。

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. .forEach()函数用于在不需要由每个数组值转换组成的新结果的情况下,对数组的每个元素执行“某些操作”。 The .map() function, on the other hand, is for transforming an array into a new array based on a transformation of each array element. .map()函数,在另一方面, 是一种用于将一个阵列到根据每个阵元的一个变换的新阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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