0

I'm trying to make my website bilingual. so I would like to iterate my JS object translation. I can iterate it with this code :

for (var word of translation) {
    console.log(word);
}   

but then I get this:

from the moment I use this code I undefined

for (var word of translation) {
    console.log(word[0]);
}   

this is my object model

    translation = [ 
      { English : [ ["English" , "engels"], ["lblEng",""] ]}, 
      {dutch : [["dutch" , "nederlands"], ["lblNl",""]]},
      { second : [["second","seconde"], ["sec",""]]},
      {minut : [["minut", "minut"],["min",""]]},
      { hour : [["hour","uur"],["h",""]]},
      { day :[ ["day", "dag"],["d",""]]}, 
      { to : [["to", "naar"],["to",""]]},
      { from : [["from", "van"],["from",""]]}
    ]
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Does this answer your question? [How to loop through an array containing objects and access their properties](https://stackoverflow.com/questions/16626735/how-to-loop-through-an-array-containing-objects-and-access-their-properties) – Krzysztof Krzeszewski Sep 10 '20 at 12:20
  • 1
    According to your example, `word` will be an *object*. None of the objects in your array have a property `0`, so `word[0]` will indeed return `undefined`. All objects have different properties. `word.English` would work for the first one, but not the second one. Since you are not explaining what output you want to get I don't know how to help. – Felix Kling Sep 10 '20 at 12:20
  • You might want to use `Object.values(word)[0]` – Endless Sep 10 '20 at 12:22

3 Answers3

0

for...of interates over the values in your array, i.e. the objects. For example the first iteration, word = { English : [ ["English" , "engels"], ["lblEng",""] ]}

So when you do word[0] its undefined as there is no 0 property in that word object

andy mccullough
  • 9,070
  • 6
  • 32
  • 55
0
for (var word of translation) {
    console.log(word[0]);
}  

The variable word will have assigned an object as follow:

{ English : [ ["English" , "engels"], ["lblEng",""] ]}

So, word[0] return undefined because the objects don't have a property 0.

Probably, what you really want to do is the following:

const translation = [ 
  { English : [ ["English" , "engels"], ["lblEng",""] ]}, 
  {dutch : [["dutch" , "nederlands"], ["lblNl",""]]},
  { second : [["second","seconde"], ["sec",""]]},
  {minut : [["minut", "minut"],["min",""]]},
  { hour : [["hour","uur"],["h",""]]},
  { day :[ ["day", "dag"],["d",""]]}, 
  { to : [["to", "naar"],["to",""]]},
  { from : [["from", "van"],["from",""]]}
];
    
for (let word of translation) {
    // get the keys and values and loop over them.
    Object.entries(word).forEach(([lang, values]) => {
        //values[0] returns the first index, so you can access it as follow:
        let [first] = values;
        console.log(lang, first);
        
    });
}
Ele
  • 33,468
  • 7
  • 37
  • 75
0

Thanks for al your reactions / answerds...

Of realy helped me forward, if took a while to respond becose I was making an ather partition of my web aplication.