DaveDavidson
DaveDavidson

Reputation: 206

Get element from mapped array

I am using console.log('errors: ' + password.get('errors')); to see what is returned from password.get('errors')); 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 "validation.password.tooLong" 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 [Map... above.

Upvotes: 0

Views: 77

Answers (3)

Kirill Shumilov
Kirill Shumilov

Reputation: 307

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).

Upvotes: 2

Shubham Khatri
Shubham Khatri

Reputation: 281794

To get the value from map you need to use get() function like

var res = password.get('errors');
var ans   = res[0].get('id');

Upvotes: 0

Ngoan Tran
Ngoan Tran

Reputation: 1527

You can try with below code:

var response = password.get('errors');
var result   = response[0]['id'];

Upvotes: 0

Related Questions