简体   繁体   English

根据使用 Javascript Map 或过滤器方法从复杂的 JSON 结构中选择 id 来显示国家/地区

[英]display countries based on selection of id from a complex JSON structure using Javascript Map or Filter Method

I have a JSON structure which is bit complex and I want to display the corresponding items based on the selection of Id.我有一个有点复杂的 JSON 结构,我想根据 Id 的选择显示相应的项目。 For example, in my JSON structure I have the sections of continent and under each continent countries are listed within the property names "Levels".So, I want to filter each continent based on the id from the "Levels" property.例如,在我的 JSON 结构中,我有大陆的部分,每个大陆下的国家都列在属性名称“Levels”中。所以,我想根据“Levels”属性中的 id 过滤每个大陆。 So, if the "Country.Level.Id === 6" it should display the Country.Name="America".因此,如果“Country.Level.Id === 6”应该显示 Country.Name="America"。 I have been trying to make it work by using JavaScript Map and Filter method but couldn't make it work.我一直在尝试通过使用 JavaScript Map 和 Filter 方法使其工作,但无法使其工作。

Here is my SandBox Link: https://codesandbox.io/s/dark-feather-ugvxv?file=/src/index.js:0-1294这是我的沙盒链接: https://codesandbox.io/s/dark-feather-ugvxv?file=/src/index.js:0-1294

The data:数据:

const items = [
  {
    Country: [
      {
        Id: "1",
        Name: "Europe",
        Levels: [
          {
            Id: "1",
            Name: "Finland",
            DisplayName: "Finland"
          }
        ]
      },
      {
        Id: "1",
        Name: "ASIA",
        Levels: [
          {
            Id: "2",
            Name: "Bangladesh",
            DisplayName: "Bangladesh"
          },
          {
            Id: "3",
            Name: "India",
            DisplayName: "India"
          },
          {
            Id: "4",
            Name: "Pakistan",
            DisplayName: "Pakistan"
          }
        ]
      },
      {
        Id: "3",
        Name: "America",
        Levels: [
          {
            Id: "5",
            Name: "USA",
            DisplayName: "USA"
          },
          {
            Id: "6",
            Name: "Canada",
            DisplayName: "Canada"
          },
          {
            Id: "7",
            Name: "Australia",
            DisplayName: "Australia"
          }
        ]
      },
      {
        Id: "4",
        Name: "Africa",
        DisplayName: "Africa",
        Levels: [
          {
            Id: "8",
            Name: "Nigeria",
            DisplayName: "Nigeria"
          }
        ]
      }
    ]
  }
];

My coding attempt:我的编码尝试:

const selectById = "5";
const countries = items[0].Country;
const result = countries.filter(item => item.Levels.Id === selectById);
console.log(result);

According to your data structure, this function will find a continent from a country id:根据您的数据结构,这个 function 将从国家 ID 中找到一个大陆:

function findContinentByCountryId(items, id) {
  return items[0].Country.find((continent) => {
    return continent.Levels.some(country => country.Id === id)
  })
}

You can test it with the snippet below:您可以使用以下代码段对其进行测试:

 function findContinentByCountryId(items, id) { return items[0].Country.find((continent) => { return continent.Levels.some(country => country.Id === id) }) }
 <form> <label for="country-id">Country id:</label> <input id="country-id"> <input type="submit" value="Find continent by country id"> </form> <span></span> <script> const items = [ { Country: [ { Id: "1", Name: "Europe", Levels: [ { Id: "1", Name: "Finland", DisplayName: "Finland" } ] }, { Id: "1", Name: "ASIA", Levels: [ { Id: "2", Name: "Bangladesh", DisplayName: "Bangladesh" }, { Id: "3", Name: "India", DisplayName: "India" }, { Id: "4", Name: "Pakistan", DisplayName: "Pakistan" } ] }, { Id: "3", Name: "America", Levels: [ { Id: "5", Name: "USA", DisplayName: "USA" }, { Id: "6", Name: "Canada", DisplayName: "Canada" }, { Id: "7", Name: "Australia", DisplayName: "Australia" } ] }, { Id: "4", Name: "Africa", DisplayName: "Africa", Levels: [ { Id: "8", Name: "Nigeria", DisplayName: "Nigeria" } ] } ] } ]; const form = document.querySelector('form') const input = document.querySelector('input') const result = document.querySelector('span') form.addEventListener('submit', (event) => { event.preventDefault() const continent = findContinentByCountryId(items, input.value) result.textContent = continent? continent.Name: 'Continent not found' }) </script>

Side note边注

I don't know the context of your problem, but be aware that object properties are usually camelCased in javascript (instead of PascalCased in your case) and that Country and Levels might not be ideal names for the corresponding data.我不知道您的问题的上下文,但请注意 object 属性通常在 javascript 中采用驼峰式命名(而不是您的情况下的PascalCased ),并且CountryLevels可能不是相应数据的理想名称。 Furthermore, in case this wasn't intentional, Europe and Asia have the same id in your dataset.此外,如果这不是故意的,欧洲和亚洲在您的数据集中具有相同的 id。

You have to change your filter method appropriately it'll work你必须适当地改变你的过滤方法它会起作用

countries.filter(item => item.Levels.find(elem => elem.Id === selectById))

SandboxLink 沙盒链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM