Suthan Bala
Suthan Bala

Reputation: 3299

ImmutableJS Map, how to iterate over a map and return a modified result

I am trying to remove the keys listed in the skipheaders array from the submissionsInForm.

const skipHeaders = [ 'layout', 'modules', 'enabled', 'captcha', 'settings'];

  submissionsInForm.map((submission) => {
    const updatedSubmission = submission.deleteAll(skipHeaders);
    return updatedSubmission;
  });

That code doesn't seem to be working, as it is returning the full data. Is there anyway to achieve this without having to converting to Vanilla JS?

Upvotes: 0

Views: 83

Answers (1)

Mulan
Mulan

Reputation: 135197

blacklist

You could use Map#filter to remove any key/value pairs where the key appears in the blacklist

// where Map is Immutable.Map
const m = Map({a: 1, b: 2, c: 3, d: 4})

const blacklist = ['a', 'b']

m.filter((v,k) => !blacklist.includes(k))

// => {"c": 3, "d": 4}

whitelist

Your object is called submissionsInForm so I'm guessing these are user-supplied values. In such a case, I think it is much better to whitelist your fields instead of blacklisting like we did above

Notice we'll add some more fields to the input m this time but our result will be the same, without having to change the whitelist. This is good when you want to explicitly declare which fields the user can submit – all other fields will be rejected.

const m = Map({a: 1, b: 2, c: 3, d: 4, admin: 1, superuser: true})

const whitelist = ['c', 'd']

m.filter((v,k) => whitelist.includes(k))

// => {"c": 3, "d": 4}

Upvotes: 1

Related Questions