【问题标题】:Getting an object from an array of objects by object property/field: functional way? (JavaScript / ES6) [duplicate]通过对象属性/字段从对象数组中获取对象:功能方式? (JavaScript / ES6)[重复]
【发布时间】:2021-01-15 21:26:42
【问题描述】:

我有一个 JavaScript 对象数组(主要来自 Web 服务中的 JSON 数据),其中每个对象代表一个国家/地区:

let countries = [
    ...,
    {
        "id": 46,
        "type": "COUNTRY",
        "name": "Cape Verde",
        "isoCode": "CV",
        "isoNbr": "132"
    },
    {
        "id": 52,
        "type": "COUNTRY",
        "name": "Christmas Island",
        "isoCode": "CX",
        "isoNbr": "162"
    },
    {
        "id": 63,
        "type": "COUNTRY",
        "name": "Cyprus",
        "isoCode": "CY",
        "isoNbr": "196"
    },
    {
        "id": 64,
        "type": "COUNTRY",
        "name": "Czech Republic",
        "isoCode": "CZ",
        "isoNbr": "203"
    },
    {
        "id": 88,
        "type": "COUNTRY",
        "name": "Germany",
        "isoCode": "DE",
        "isoNbr": "276"
    },
    {
        "id": 66,
        "type": "COUNTRY",
        "name": "Djibouti",
        "isoCode": "DJ",
        "isoNbr": "262"
    },
    
    ...
];

问题

您如何最好地在此处获取isoCode 的条目?

也许我必须事先将其转换为地图 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map),其中 isoCode 是键,值是未更改的对象...??

let mapOfCountries = turn_into_map(countries);

selectedCountry = mapOfCountries[this.selectedEntity.countryCode];

// or even simpler:
// selectedCountry = mapOfCountries['DE'];

您如何在 JavaScript / ES6 中最好地做到这一点?也许是一种功能性方法?我不想在这里使用 for 循环并手动检查。呵呵。

【问题讨论】:

  • 要么将其转换为地图,要么使用 .find() - 节省您编写 for 循环的时间。
  • 好吧,现在这似乎是我普遍缺少的东西。

标签: javascript arrays dictionary ecmascript-6 functional-programming


【解决方案1】:

只需使用find(...) method

const countries=[{id:46,type:"COUNTRY",name:"Cape Verde",isoCode:"CV",isoNbr:"132"},{id:52,type:"COUNTRY",name:"Christmas Island",isoCode:"CX",isoNbr:"162"},{id:63,type:"COUNTRY",name:"Cyprus",isoCode:"CY",isoNbr:"196"},{id:64,type:"COUNTRY",name:"Czech Republic",isoCode:"CZ",isoNbr:"203"},{id:88,type:"COUNTRY",name:"Germany",isoCode:"DE",isoNbr:"276"},{id:66,type:"COUNTRY",name:"Djibouti",isoCode:"DJ",isoNbr:"262"}];

console.log(countries.find(c => c.isoCode === 'DJ'))

另外,供您参考 - 在提供的链接上,您可以看到更多关于 JavaScript 的资源。我建议你通读一遍,它很有趣:)

【讨论】:

    【解决方案2】:

    您可以使用Object.fromEntries 创建您的地图,然后只需通过国家/地区代码即可访问它,例如

    const mapOfCountries = Object.fromEntries(
      countries.map(o => [o.isoCode, o])
    );
    console.log(mapOfCountries['DE'])
    

    let countries = [
       { id: 46, type: "COUNTRY", name: "Cape Verde", isoCode: "CV", isoNbr: "132" },
       { id: 52, type: "COUNTRY", name: "Christmas Island", isoCode: "CX", isoNbr: "162" },
       { id: 63, type: "COUNTRY", name: "Cyprus", isoCode: "CY", isoNbr: "196" },
       { id: 64, type: "COUNTRY", name: "Czech Republic", isoCode: "CZ", isoNbr: "203" },
       { id: 88, type: "COUNTRY", name: "Germany", isoCode: "DE", isoNbr: "276" },
       { id: 66, type: "COUNTRY", name: "Djibouti", isoCode: "DJ", isoNbr: "262" }
    ];
    
    const mapOfCountries = Object.fromEntries(
      countries.map(o => [o.isoCode, o])
    );
    
    console.log(mapOfCountries['DE']);
    console.log(mapOfCountries['CZ']);

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 1970-01-01
      • 2019-05-29
      • 2018-05-25
      • 1970-01-01
      • 2013-05-05
      • 2019-02-25
      相关资源
      最近更新 更多