skip to Main Content

I have a JSON object.
Within this object I have an Object array named 'languages' and in this array there are upwards of 1 languages object listed.

How would I construct a forEach loop to access the 'name' of each 'language' Object?

Example of what the PHP cURL routine outputs:

languages: Array(4)
0: {iso639_1: "en", iso639_2: "eng", name: "English", nativeName: "English"}
1: {iso639_1: "fj", iso639_2: "fij", name: "Fijian", nativeName: "vosa Vakaviti"}
2: {iso639_1: "hi", iso639_2: "hin", name: "Hindi", nativeName: "हिन्दी"}
3: {iso639_1: "ur", iso639_2: "urd", name: "Urdu", nativeName: "اردو"}
length: 4

This is how the data is used in a popup rendered dynamically via js:

var popupCreateLanguagesResultTD = document.createElement("div");
popupCreateLanguagesResultTD.className = "col";
popupCreateLanguagesResultTD.innerText = result["languages"];

What I want it to look like: Languages Spoken: English, Fijian, Hindi, Urdu.


Example of JSON file structure:

{
  "name": "Canada",
  "topLevelDomain": [
    ".ca"
  ],
  "alpha2Code": "CA",
  "alpha3Code": "CAN",
  "callingCodes": [
    "1"
  ],
  "capital": "Ottawa",
  "altSpellings": [
    "CA"
  ],
  "region": "Americas",
  "subregion": "Northern America",
  "population": 36155487,
  "latlng": [
    60,
    -95
  ],
  "demonym": "Canadian",
  "area": 9984670,
  "gini": 32.6,
  "timezones": [
    "UTC-08:00",
    "UTC-07:00",
    "UTC-06:00",
    "UTC-05:00",
    "UTC-04:00",
    "UTC-03:30"
  ],
  "borders": [
    "USA"
  ],
  "nativeName": "Canada",
  "numericCode": "124",
  "currencies": [
    {
      "code": "CAD",
      "name": "Canadian dollar",
      "symbol": "$"
    }
  ],
  "languages": [
    {
      "iso639_1": "en",
      "iso639_2": "eng",
      "name": "English",
      "nativeName": "English"
    },
    {
      "iso639_1": "fr",
      "iso639_2": "fra",
      "name": "French",
      "nativeName": "français"
    }
  ],
  "translations": {
    "de": "Kanada",
    "es": "Canadá",
    "fr": "Canada",
    "ja": "カナダ",
    "it": "Canada",
    "br": "Canadá",
    "pt": "Canadá",
    "nl": "Canada",
    "hr": "Kanada",
    "fa": "کانادا"
  },
  "flag": "https://restcountries.eu/data/can.svg",
  "regionalBlocs": [
    {
      "acronym": "NAFTA",
      "name": "North American Free Trade Agreement",
      "otherAcronyms": [],
      "otherNames": [
        "Tratado de Libre Comercio de América del Norte",
        "Accord de Libre-échange Nord-Américain"
      ]
    }
  ],
  "cioc": "CAN"
}

EDIT: I have tried

var popupCreateLanguagesResultTD = document.createElement("div");
popupCreateLanguagesResultTD.className = "col";
result["languages"].forEach(obj => {
         Object.entries(obj).forEach(([name, value]) => {
             popupCreateLanguagesResultTD.innerText = (`${name} ${value}`);
          });
 });

But I get nativeName and then the native name of the language. e.g French -> francais.

2

Answers


  1. You’re iterating over all the properties in the object, not the languages array.

    Use map() to extract all the name properties from the objects in that array, and join() to combine them into a comma-separated string.

    popupCreateLanguagesResultTD.innerText = obj.languages.map(lang => lang.name).join(", ");
    
    Login or Signup to reply.
  2. So you have data

    languages: Array(4)
    0: {iso639_1: "en", iso639_2: "eng", name: "English", nativeName: "English"}
    1: {iso639_1: "fj", iso639_2: "fij", name: "Fijian", nativeName: "vosa Vakaviti"}
    2: {iso639_1: "hi", iso639_2: "hin", name: "Hindi", nativeName: "हिन्दी"}
    3: {iso639_1: "ur", iso639_2: "urd", name: "Urdu", nativeName: "اردو"}
    length: 4
    

    and want to output

    What I want it to look like: Languages Spoken: English, Fijian, Hindi, Urdu.
    

    i think you can do

    const langISpeak = languages.map( lang => lang.nativeName).join(', ')
    console.log('i speak in : ' + langISpeak)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search