How do I get to a nested element in an array?

  • 0
    Good evening. I need to make an array like this
    lang = [
      LANGUAGE = [
        FROM: RU,
        TO: EN
      ]
    ]


    I'm creating it, but how can I get to the RU or EN value to put this value into the persistent. I apologize for such a stupid question, but I cannot figure out how to do it.

    my code is
    let lang=[];
    
    lang.push({
              ["LAMNGUAGE"]:[["FROM","RU"],["TO","EN"]]
            })
    for(let key in lang){
    console.log(Object.values(lang)[0][0])
    }
    
    let lang1 = ????
    let lang2 = ????
    JavaScript Anonymous, Sep 13, 2020

  • 3 Answers
  • 0
    let lang=[];

    lang.push({
    ["LANGUAGE"]:[["FROM","RU"],["TO","EN"]]
    });

    const from = lang[0].LANGUAGE[0][1];
    const to = lang[0].LANGUAGE[1][1];

    console.log(from); // RU
    console.log(to); // EN
    Parker Campbell

  • 0
    let lang1 = lang[0]['LAMNGUAGE'][0][1],
    lang2 = lang[0]['LAMNGUAGE'][1][1];

    console.log(lang1, lang2);
    Anonymous

  • 0
    I think you need a structure like this:

    let langs = [
    {
    from: 'RU',
    to: 'EN'
    }
    ];

    langs.push({
    from: 'FR',
    to: 'DE'
    });

    const lang1 = langs[0].from; // RU
    const lang2 = langs[0].to; // EN
    Victoria Guerra

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!