【发布时间】:2018-11-29 10:23:58
【问题描述】:
我正在编写一个 ReactJS 应用程序,并且我在函数 filterSelected 中有一个 for 循环,它通过区域名称数组(例如["Thailand", "Malaysia"])并应该返回它们相应区域代码的数组(例如。 ["TH", "MY"])。
在我的mapRegionToCode 函数中,它通过对象的对象进行此映射,并且控制台日志正确打印相应的区域代码,但是该函数返回undefined。
任何想法为什么将不胜感激!
mapRegionToCode(region) { // region = "Thailand"
_.forEach(_.values(this.props.regionsCodes), regionCode => {
if(regionCode.country.countryName === region) {
console.log(regionCode.country.countryCode); //"TH"
return regionCode.country.countryCode;
}
});
}
filterSelected(filters) {
...
for(let i = 0; i < regionsArray.length; i++){
const regionCode = this.mapRegionToCode(regionName);
console.log(regionCode); //undefined
regionNames.push(regionCode);
}
...
}
【问题讨论】:
-
我想你的意思是_.map 不是
forEach
标签: javascript reactjs for-loop return lodash