0

i need to flat the object values in javascript . the ent is the input array . the first object the language consist of array of array values i need to flat the object value .[[{}],[{}],[{}]]

experted the value to be language: [{},{},{}];




const ent =[{
    "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"
  },
  ];

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

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


the Ouput is the array with the selected keys value is return but i need to flat the value of the object

 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 need to flat the object value .[[{}],[{}],[{}]]

experted the value to be language: [{},{},{}];

i have tried to do like this:

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

const output = ent.map(item => outputKeys.reduce((val, key) => ({ ...val, [key]: item[key].flat(infinity) }), {}) ); console.log(output);

look out the first object the language consist the nested arrays ! thanks in advance .

5
  • Show us what you have tried. SO isn't a free code writing service. The objective here is for you to post your attempts to solve your own issue and others help when they don't work as expected. See How to Ask and minimal reproducible example Commented Dec 25, 2020 at 13:12
  • @charlietfl sorry im new to stackoverflow its my first question my bad i dint know that ! Commented Dec 25, 2020 at 13:15
  • @charlietfl i have added my attempt now please have a look over it Commented Dec 25, 2020 at 13:18
  • you only want to do this for the languages object ? or for every object Commented Dec 25, 2020 at 13:19
  • @Ashu i want to do if the key consist of nested array it should be faltted ! Commented Dec 25, 2020 at 13:21

1 Answer 1

1

You can use destructuring to only select specific properties, and then .flat only the languages array.

const ent =[{
    "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"
  },
  ];
  
  
const output = ent.map(({name, capital, region, languages, flag}) => ({
  name,
  capital,
  region,
  languages: languages.flat(),
  flag
}));

console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.