1

I have a Vanilla JS array, containing numbers as strings.

const stringArray = ['1', '2', '3', '4'];

If I want to convert all the numbers in the array to intger I simply do

const integerArray = stringArray.map(Number);

Now, I have an Immutable JS List

const immutableListStrings = Immutable.List(stringArray);

Using

immutableListStrings.map(Number);

does not convert the strings to integer. Why is that?

2
  • 1
    I have no knowledge about immutable.js, but in vanilla JS Array.prototype.map does not change the array in place. Commented Oct 17, 2019 at 8:59
  • Just like with standard map, Immutable's .map returns a new List (immutable-js.github.io/immutable-js/docs/#/List/map). In fact, the whole point of the library is to create/use immutable data structures so operations usually return a new value. Commented Oct 17, 2019 at 9:02

1 Answer 1

2

map in general literally maps through your list values and allows you to transform them before putting them into a new list.

map does not transform your current list.

You simply need to create a new variable (i.e.: immutableNumbers)

const stringArray = ['1', '2', '3', '4'];
const immutableListStrings = Immutable.List(stringArray);
const immutableNumbers = immutableListStrings.map(Number);

console.log(immutableNumbers);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>

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.