Wilbur Robertson
Wilbur Robertson

Reputation: 367

How to create an immutable map from .map() JS

So I have a function like this

myNewMap = oldMap.map((check) => {
     if (check.get('_id') === against.get('_id')) return check;
});

The only problem is myNewMap is the same size as the oldMap (which makes sense), but is there a way to stop this, or do I need a different approach?

Upvotes: 1

Views: 509

Answers (2)

andre mcgruder
andre mcgruder

Reputation: 1520

After you map over the array you can do

Object.seal( yourMappedArrayObject );

That will make that object then immutable.

Object.seal

Upvotes: 0

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23502

I think you want filter

myNewMap = oldMap.filter((check) => {
   return check.get('_id') === against.get('_id');
});

Or even shorter:

myNewMap = oldMap.filter(check => check.get('_id') === against.get('_id'));

This will return a new Map with only the values that meet the predicate.

Upvotes: 3

Related Questions