简体   繁体   中英

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. Thanks in advance.

I need to know what are the different ways to do that in JavaScript!

Using array methods or raw object and loops!

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

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. The even complex array of object examples with answers also appreciated. thank you.

You can use Javascript Array functions map and reduce to filter keys from input array. In the below example you can change the outputKeys array to get the wanted output.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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