0

Im debugging a React app, and I have this element here:

enter image description here

If I try to make a user.matches[1] the console returns me the element who is in this position.

But If I try to make a map or a find in this array, the console says "undefined".

enter image description here

What Im missing here??? I want to make a for on this object. Why I cant?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Fernando Maymone
  • 665
  • 1
  • 8
  • 17
  • 2
    Looks like `user.matches` is not an array. Use `Object.entries` etc instead. (Call `Array.isArray` on it, see what happens) – CertainPerformance Jun 05 '18 at 00:16
  • You can see in the first screenshot already that `user.matches` is an object. If it was an array, the output would be something like `[{...}, {...}]`, not `{1: {...}, 2: {...}}`. – Felix Kling Jun 05 '18 at 00:18
  • try and find the type of that, it's not an array im pretty sure. – Sheshank S. Jun 05 '18 at 00:34

1 Answers1

0

As said by @CertianPerformance that is not an array. that is an object. {} not []. What you need to do is count the keys.

This can be found here How to list the properties of a JavaScript object?

var keys = Object.keys(myObject);

then keys would be an array :)

edit: said object instead of array

Chris
  • 82
  • 12
  • 1
    No, `keys` would be an array. – Felix Kling Jun 05 '18 at 00:42
  • @FelixKling yup good catch. this is why i should reread things. – Chris Jun 05 '18 at 00:50
  • Big problem here. Because I need to map all the values inside this structure. For example, on that structure I have a attribute called "group" . And sometimes I need to filter by this atribute (with a find(k=>k===GROUP_A". If I go a Object.keys I have a useless array with: "1", "2", "3", "4", "5", "6", "7". If I do a Object entries, I have other useless array (because I cant make a find) with: (66) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2). What to do? – Fernando Maymone Jun 05 '18 at 09:19
  • With being able to get the key length you can now just make an array :) meaning take the length of the keys and push it into an array because you can now loop through and do somearray.push(user.matches[i]) – Chris Jun 05 '18 at 20:29