DaveDavidson DaveDavidson - 9 months ago 65
React JSX Question

Get element from mapped array

I am using

console.log('errors: ' + password.get('errors'));
to see what is returned from
and in the console this is returned:

List [ Map { "id": "validation.password.tooLong", "defaultMessage": "La password deve avere massimo {max} caratteri", "values": Map { "max": 16 } } ]

I want to access the element
from this mapped array, I am unsure how exactly to do this. I am not 100% sure this is a mapped array, I am assuming it is though because of the

Answer Source

I assume that you are using immutable.js, thus to get the desired data you need to access this property via methods of these classes:

const errors = password.get('errors');
const errorId = errors.get(0).get('id');

In other answers, you got an undefined because List is an instance of class List, that haven't property 0, but have a method get that returns a value from an array, that stored in the closure. It's a special solution to prevent mutating and ensure immutability (if you want to update value, you should use set(0, value) instead of myList[0] = value, so there is no access via [0]). The same thing with the Map (Map is an immutable object, that stores key: value).

You can learn more about it here: Immutable.js docs (but I'm not sure you have exactly immutable.js, there are many similar libraries, so take a look what do you use exactly).