0

I have result array of objects with such structure: enter image description here And I'm trying to retrieve "login" field iterating this result. I do next:

for(key in result) {
     console.log(key);
}

and it prints only object indexes: 0, 1, 2... How can I retrieve object fields? I'm new in JS.

Nikolas
  • 2,322
  • 9
  • 33
  • 55
  • you already have the index so just do result[key] then you can get login with result[key].login. Array.map might be a better option – Chris Li Sep 09 '18 at 10:50
  • 1
    `for-in` isn't how you loop through arrays, see the linked dupetarget. To access these `login` properties on the objects in the array, you'd loop through it, and use the `.login` property of each entry in the array. There are several ways to do that, one of them would be: `for (const entry of result) { console.log(entry.login); }`. (But again, there are lots of ways. If you *only* care about `login`, you might add destructuring: `for (const {login} of result) { console.log(login); }` Or if you need to support ES5-only environments, you might use `forEach` instead.) – T.J. Crowder Sep 09 '18 at 10:50
  • This is what I tried to do! Thank you! – Nikolas Sep 09 '18 at 10:55

0 Answers0