1

I have my form field values in an immutable object.
I use getIn immutable function to access it.

For example, if I have to access field, i use const users = formFields.getIn(['0', value]).
Now, i have a variable

users = 4`

This means, there will be 4 fields in immutable from which i need to pick up the users age.

e.g.

  • 1st user age will be stored in formFields.getIn(['1', value])
  • 2nd user age will be stored in formFields.getIn(['2', value])
  • and so on

How do i loop through the user age list based on the users variable?
I tried something like this:

const userAgeList = [];
if (users >0) {
  userAgeList.push(formFields.getIn([[i], value]));
}

With above code formFields.getIn([[i], value]), i get undefined because the value is not actually on this. its on formFields.getIn(['i', value]).

How do i pass the loop variable i as a string so i can get the field values?

6
  • [i] this makes an array with one value, the value of i. Is this what you're expecting? Commented Oct 20, 2020 at 11:58
  • 1
    Have you tried to use template strings? Like ` ${i} ` ? Commented Oct 20, 2020 at 11:59
  • @evolutionxbox- if i just use i, it will be number. i want to use 'i' Commented Oct 20, 2020 at 12:00
  • 1
    @ashwinprabhu, Did you tried like, formFields.getIn([`${i}`, value]) ?? Commented Oct 20, 2020 at 12:00
  • 3
    formFields.getIn([i.toString(), value])? Commented Oct 20, 2020 at 12:01

1 Answer 1

2

If what you have is a List containing Map objects, you can use a map to loop all the values:

const userAgeList = formFields
  .map(field -> field.get('value'))
  .toArray()

This will give you an array of the values you need.
If you want to take only at a specific i, convert it to a number and then you can combine skip and take in this fashion:

const userAgeList = formFields
  .skip(i)
  .take(1)
  .map(field -> field.get('value'))
  .toArray()

This will return you a one element array at the i position.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.