【问题标题】:How to find a key by value of a child element?如何通过子元素的值查找键?
【发布时间】:2020-11-14 15:28:53
【问题描述】:

我的 JSON 文件 (countries_numbers.json):

{
  "AF": {
    "countryName": "Afghanistan",
    "countryPrefix": "93"
  },
  "AL": {
    "countryName": "Albania",
    "countryPrefix": "355"
  },
  "DZ": {
    "countryName": "Algeria",
    "countryPrefix": "213"
  },
  "AS": {
    "countryName": "American Samoa",
    "countryPrefix": "1"
  },
  "AD": {
    "countryName": "Andorra",
    "countryPrefix": "376"
  },
  "AO": {
    "countryName": "Angola",
    "countryPrefix": "244"
  },
  "AI": {
    "countryName": "Anguilla",
    "countryPrefix": "1"
  },
  "AG": {
    "countryName": "Antigua",
    "countryPrefix": "1"
  },
  "AR": {
    "countryName": "Argentina",
    "countryPrefix": "54"
  },
  "AM": {
    "countryName": "Armenia",
    "countryPrefix": "374"
  },
  "AW": {
    "countryName": "Aruba",
    "countryPrefix": "297"
  },
  "AU": {
    "countryName": "Australia",
    "countryPrefix": "61"
  },
  "AT": {
    "countryName": "Austria",
    "countryPrefix": "43"
  },
  "AZ": {
    "countryName": "Azerbaijan",
    "countryPrefix": "994"
  },
  "BH": {
    "countryName": "Bahrain",
    "countryPrefix": "973"
  },
  "BD": {
    "countryName": "Bangladesh",
    "countryPrefix": "880"
  },
  "BB": {
    "countryName": "Barbados",
    "countryPrefix": "1"
  },
  "BY": {
    "countryName": "Belarus",
    "countryPrefix": "375"
  },
  "BE": {
    "countryName": "Belgium",
    "countryPrefix": "32"
  },
  "BZ": {
    "countryName": "Belize",
    "countryPrefix": "501"
  },
  "BJ": {
    "countryName": "Benin",
    "countryPrefix": "229"
  },
  "BM": {
    "countryName": "Bermuda",
    "countryPrefix": "1"
  },
  "BT": {
    "countryName": "Bhutan",
    "countryPrefix": "975"
  },
  "BO": {
    "countryName": "Bolivia",
    "countryPrefix": "591"
  },
  "BA": {
    "countryName": "Bosnia and Herzegovina",
    "countryPrefix": "387"
  },
  "BW": {
    "countryName": "Botswana",
    "countryPrefix": "267"
  },
  "BR": {
    "countryName": "Brazil",
    "countryPrefix": "55"
  },
  "IO": {
    "countryName": "British Indian Ocean Territory",
    "countryPrefix": "246"
  },
  "VG": {
    "countryName": "British Virgin Islands",
    "countryPrefix": "1"
  },
  "BN": {
    "countryName": "Brunei",
    "countryPrefix": "673"
  },
  "BG": {
    "countryName": "Bulgaria",
    "countryPrefix": "359"
  },
  "BF": {
    "countryName": "Burkina Faso",
    "countryPrefix": "226"
  },
  "MM": {
    "countryName": "Burma Myanmar",
    "countryPrefix": "95"
  }
}

现在我想在这个 JSON 文件中搜索一个值。类似的东西:

搜索:countryPrefix = "226" ¦ 找到返回时:"BF" 否则返回 "false"

我希望你新我想做的事。抱歉这个不好的问题,我是 JavaScript 的新手。

PS:我已经在谷歌上搜索过,什么也没找到。

【问题讨论】:

    标签: javascript json search


    【解决方案1】:

    一种可能的方法:

    const countries = {
      "AF": {
        "countryName": "Afghanistan",
        "countryPrefix": "93"
      },
      "AL": {
        "countryName": "Albania",
        "countryPrefix": "355"
      }
      // .. the rest cut for brevity
    }
    
    const getCodeByPrefix = prefix => 
      Object.keys(countries).find(code => countries[code].countryPrefix === prefix);
    
    console.log(getCodeByPrefix('93')); // AF
    console.log(getCodeByPrefix('193')); // undefined

    这里的getCodeByPrefix是一个以前缀为参数的函数;如果没有找到给定前缀的值,则它的返回值是代码 - 或 undefined


    你在这里所做的是被称为reverse lookup:试图通过它的值找到一个特定的键。此操作的复杂度为 O(n),这意味着您的对象中的国家越多,就越难找到。

    现在,由于我们仍在讨论数百个条目,而不是数千个(更不用说数十万个),所以这并不重要。我仍然建议您考虑反转数据结构以制作前缀键 - 单个或重复键。

    【讨论】:

      【解决方案2】:

      虽然 raina77ow 可能回答了你的问题,但我想我分享了另一种方法。

      for (var key in json) {
          if (json.hasOwnProperty(key)) {
                      if(json[key]['countryPrefix'] == 226) {
                          // do stuff
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-12
        • 2016-01-14
        相关资源
        最近更新 更多