【问题标题】:how could i return the value inside of a foreach javascript我如何返回 foreach javascript 中的值
【发布时间】: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呢?

【问题讨论】:

标签: javascript arrays reactjs sorting ecmascript-6


【解决方案1】:

你不能。 forEach 不打算返回任何内容,但您可以使用另一个函数从数组中获取 id

使用find 将返回一个满足您条件的对象。

getCountry(countryName) {
    return country_list_with_id.find(item => item.name.toLowerCase() === countryName);
}

这将返回 country 对象,您可以从该对象注入 id。如果未找到任何内容,则返回 undefined。所以你需要先检查那个对象,然后再尝试访问它的属性。

const country = array.getCountry("USA");

console.log(country && country.id);

【讨论】:

  • 如果您不关心史前浏览器,这是最合乎逻辑的解决方案 :-) 干得好。
  • @user3492940 if you don't care about prehistoric browsers 在 SO,我们不这样做。对于这些过时和安全发布的浏览器,我们 pollyfill & transpile.. :)
  • 最近看了一篇博客,有人说史前浏览器不用管,强制他们升级浏览器:)。尽管如此,Javascript 必须使用转译版本和 polyfills 进行生产
  • @SurenSrapyan 确实,否则我们将花费 50% 的时间来回答问题,方法是编写代码来展示它在旧浏览器中是如何完成的。 IMO:除非 OP 另有说明,否则所有答案都应该假设 ES6 没问题。
  • 不是说你应该关心它们,而是值得一提的事情,因为不是每个人都有忽略我们中间恐龙的奢侈。
【解决方案2】:

这里使用需要使用.filter。这将返回符合特定条件的数组中的项目

 var data = [{ id: 2, code: "AL", name: "Albania" }, { id: 3, code: "DZ", name: "Algeria" }, { id: 4, code: "DS", name: "American Samoa" }]

Array.prototype.getCountryID = function(code){
  var output = this.filter(el => el.code === code);
  return output.length > 0 ? output[0].id : "Not Found";
}

console.log(data.getCountryID("DS"));
console.log(data.getCountryID("something else"));

【讨论】:

    【解决方案3】:

    您可以filter您的国家/地区数组以获取您想要的国家/地区,并返回结果。 countries[0] 可能是 undefined,因此请使用我示例中的 if 语句或 @void 示例中的三元运算符这是 sn-p:

    const countries = [{ id: 2, code: "AL", name: "Albania" }, { id: 3, code: "DZ", name: "Algeria" }, { id: 4, code: "DS", name: "American Samoa" }];
    function getCountryId(code) {
      const country = countries.filter(country => country.code === code);
      if(country.length > 0) {
        return country[0].name;
      } else {
        return "No such country.";
      }
    }
    console.log(getCountryId("DZ"));
    console.log(getCountryId("USA"));

    【讨论】:

      【解决方案4】:

      您可以使用Array.prototype.filter 按名称过滤出国家/地区,然后返回first/last 项的id

      const list = [{
        id: 2,
        code: "AL",
        name: "Albania"
      }, {
        id: 3,
        code: "DZ",
        name: "Algeria"
      }, {
        id: 4,
        code: "DS",
        name: "American Samoa"
      }];
      
      
      function getCountryId(name) {
        return (list.filter((country) => country.name === name)[0] || {}).id;
      }
      
      
      console.log(getCountryId('Algeria'));
      console.log(getCountryId('NoneExistingCountry'));

      【讨论】:

        猜你喜欢
        • 2016-10-08
        • 2016-09-26
        • 1970-01-01
        • 2017-05-23
        • 1970-01-01
        • 2016-01-21
        • 1970-01-01
        • 2019-10-09
        相关资源
        最近更新 更多