简体   繁体   English

如何从 JavaScript 中的嵌套对象数组中获取选择性数据

[英]How to get the selective data from the array of nested objects in JavaScript

I need the specific data from the input array.我需要输入数组中的特定数据。 The input array is given below.输入数组如下所示。 and the excepted output array is given below using JavaScript.下面使用 JavaScript 给出了例外的 output 阵列。 Thanks in advance.提前致谢。

I need to know what are the different ways to do that in JavaScript!我需要知道在 JavaScript 中有哪些不同的方法来做到这一点!

Using array methods or raw object and loops!使用数组方法或原始 object 和循环!

I need to know what is the efficient ways!我需要知道什么是有效的方法!

const input = [
  {
    name: 'Afghanistan',
    topLevelDomain: ['.af'],
    alpha2Code: 'AF',
    alpha3Code: 'AFG',
    callingCodes: ['93'],
    capital: 'Kabul',
    altSpellings: ['AF', 'Afġānistān'],
    region: 'Asia',
    subregion: 'Southern Asia',
    population: 27657145,
    latlng: [33, 65],
    demonym: 'Afghan',
    area: 652230,
    gini: 27.8,
    timezones: ['UTC+04:30'],
    borders: ['IRN', 'PAK', 'TKM', 'UZB', 'TJK', 'CHN'],
    nativeName: 'افغانستان',
    numericCode: '004',
    currencies: [
      {
        code: 'AFN',
        name: 'Afghan afghani',
        symbol: '؋'
      }
    ],
    languages: [
      {
        iso639_1: 'ps',
        iso639_2: 'pus',
        name: 'Pashto',
        nativeName: 'پښتو'
      },
      {
        iso639_1: 'uz',
        iso639_2: 'uzb',
        name: 'Uzbek',
        nativeName: 'Oʻzbek'
      },
      {
        iso639_1: 'tk',
        iso639_2: 'tuk',
        name: 'Turkmen',
        nativeName: 'Türkmen'
      }
    ],
    translations: {
      de: 'Afghanistan',
      es: 'Afganistán',
      fr: 'Afghanistan',
      ja: 'アフガニスタン',
      it: 'Afghanistan',
      br: 'Afeganistão',
      pt: 'Afeganistão',
      nl: 'Afghanistan',
      hr: 'Afganistan',
      fa: 'افغانستان'
    },
    flag: 'https://restcountries.eu/data/afg.svg',
    regionalBlocs: [
      {
        acronym: 'SAARC',
        name: 'South Asian Association for Regional Cooperation',
        otherAcronyms: [],
        otherNames: []
      }
    ],
    cioc: 'AFG'
  },
  {
    name: 'Åland Islands',
    topLevelDomain: ['.ax'],
    alpha2Code: 'AX',
    alpha3Code: 'ALA',
    callingCodes: ['358'],
    capital: 'Mariehamn',
    altSpellings: ['AX', 'Aaland', 'Aland', 'Ahvenanmaa'],
    region: 'Europe',
    subregion: 'Northern Europe',
    population: 28875,
    latlng: [60.116667, 19.9],
    demonym: 'Ålandish',
    area: 1580,
    gini: null,
    timezones: ['UTC+02:00'],
    borders: [],
    nativeName: 'Åland',
    numericCode: '248',
    currencies: [
      {
        code: 'EUR',
        name: 'Euro',
        symbol: '€'
      }
    ],
    languages: [
      {
        iso639_1: 'sv',
        iso639_2: 'swe',
        name: 'Swedish',
        nativeName: 'svenska'
      }
    ],
    translations: {
      de: 'Åland',
      es: 'Alandia',
      fr: 'Åland',
      ja: 'オーランド諸島',
      it: 'Isole Aland',
      br: 'Ilhas de Aland',
      pt: 'Ilhas de Aland',
      nl: 'Ålandeilanden',
      hr: 'Ålandski otoci',
      fa: 'جزایر الند'
    },
    flag: 'https://restcountries.eu/data/ala.svg',
    regionalBlocs: [
      {
        acronym: 'EU',
        name: 'European Union',
        otherAcronyms: [],
        otherNames: []
      }
    ],
    cioc: ''
  },
  {
    name: 'Albania',
    topLevelDomain: ['.al'],
    alpha2Code: 'AL',
    alpha3Code: 'ALB',
    callingCodes: ['355'],
    capital: 'Tirana',
    altSpellings: ['AL', 'Shqipëri', 'Shqipëria', 'Shqipnia'],
    region: 'Europe',
    subregion: 'Southern Europe',
    population: 2886026,
    latlng: [41, 20],
    demonym: 'Albanian',
    area: 28748,
    gini: 34.5,
    timezones: ['UTC+01:00'],
    borders: ['MNE', 'GRC', 'MKD', 'KOS'],
    nativeName: 'Shqipëria',
    numericCode: '008',
    currencies: [
      {
        code: 'ALL',
        name: 'Albanian lek',
        symbol: 'L'
      }
    ],
    languages: [
      {
        iso639_1: 'sq',
        iso639_2: 'sqi',
        name: 'Albanian',
        nativeName: 'Shqip'
      }
    ],
    translations: {
      de: 'Albanien',
      es: 'Albania',
      fr: 'Albanie',
      ja: 'アルバニア',
      it: 'Albania',
      br: 'Albânia',
      pt: 'Albânia',
      nl: 'Albanië',
      hr: 'Albanija',
      fa: 'آلبانی'
    },
    flag: 'https://restcountries.eu/data/alb.svg',
    regionalBlocs: [
      {
        acronym: 'CEFTA',
        name: 'Central European Free Trade Agreement',
        otherAcronyms: [],
        otherNames: []
      }
    ],
    cioc: 'ALB'
  }
];

and i need the output array like this我需要像这样的 output 阵列

const output = [
  {
    name: 'Afghanistan',
    capital: 'Kabul',
    region: 'Asia',
    languages: [
      {
        iso639_1: 'ps',
        iso639_2: 'pus',
        name: 'Pashto',
        nativeName: 'پښتو'
      },
      {
        iso639_1: 'uz',
        iso639_2: 'uzb',
        name: 'Uzbek',
        nativeName: 'Oʻzbek'
      },
      {
        iso639_1: 'tk',
        iso639_2: 'tuk',
        name: 'Turkmen',
        nativeName: 'Türkmen'
      }
    ],
    flag: 'https://restcountries.eu/data/afg.svg'
  },
  {
    name: 'Åland Islands',
    capital: 'Mariehamn',
    region: 'Europe',
    languages: [
      {
        iso639_1: 'sv',
        iso639_2: 'swe',
        name: 'Swedish',
        nativeName: 'svenska'
      }
    ],
    flag: 'https://restcountries.eu/data/ala.svg'
  },
  {
    name: 'Albania',
    capital: 'Tirana',
    region: 'Europe',
    languages: [
      {
        iso639_1: 'sq',
        iso639_2: 'sqi',
        name: 'Albanian',
        nativeName: 'Shqip'
      }
    ],
    flag: 'https://restcountries.eu/data/alb.svg'
  }
];

I have tried to get the specific values from the array of object but I can able to do it.我试图从 object 数组中获取特定值,但我可以做到。 The even complex array of object examples with answers also appreciated.带有答案的 object 示例的偶数复杂数组也值得赞赏。 thank you.谢谢你。

You can use Javascript Array functions map and reduce to filter keys from input array.您可以使用 Javascript 数组函数mapreduce以从输入数组中过滤键。 In the below example you can change the outputKeys array to get the wanted output.在下面的示例中,您可以更改outputKeys数组以获取所需的 output。

const outputKeys = ['name', 'capital', 'region', 'languages', 'flag'];

const output = input.map(item =>
  outputKeys.reduce((val, key) => ({ ...val, [key]: item[key] }), {})
);

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

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