【发布时间】:2018-08-17 22:04:35
【问题描述】:
我有一个函数需要返回一个国家的 id。
我的列表是一个对象数组:
{
id: 2,
code: "AL",
name: "Albania"
}...
这是我从国家/地区获取所需 id 的功能
getCountryID(countryName) {
return country_list_with_id.forEach(res => {
if (res.name.toLowerCase() === countryName) {
console.log("the id is",res.id)// the log is 143
let id = res.id;
return id;
}
});
}
console.log(array.getCountryID("USA"))//undefined
那我怎么才能得到这个id呢?
【问题讨论】:
-
toLowerCase() === countryNamecountryName = "USA"永远不会发生..
标签: javascript arrays reactjs sorting ecmascript-6