1

I have this Map

let map = new Map(Object.entries({
   a: 1,
   b: {
        c: 2,
        method() {console.log('test')}
      }
   }
));

Now, I want to change map.b.method. How can I achieve that?

5
  • Get the value of key "b" and update the "method" property? Commented Jul 16, 2018 at 16:16
  • Have you tried, map.get('b').method = ...? Commented Jul 16, 2018 at 16:17
  • Thanks, but is it not possible with map.set()? Commented Jul 16, 2018 at 16:18
  • @UnknownJoe map.set('b', { ...map.get('b'), method() { … } }) Commented Jul 16, 2018 at 16:21
  • A Map is, and I'm being really broad here, basically just an Object that allows you to use things other than Strings as keys. How would you do this with a regular Object? Start there and tell us what issues you run into. Commented Jul 16, 2018 at 16:22

1 Answer 1

6

It's just an object stored inside the map. Get a reference to it and modify it as you'd like.

let map = new Map(Object.entries({
  a: 1,
  b: {
    c: 2,
    method() {
      console.log('test')
    }
  }
}));

map.set('b', {
  ...map.get('b'),
  method: function() {
    console.log('It works ;)');
  }
});

map.get('b').method();

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.