0

In my React app, at one point I'm trying to copy a Map from an object, but it's not working for me.

The initial user object has the structure:

user: { videoTracks: Map, ... other fields... }

If I do a console log on user, I get this for videoTracks:

enter image description here

Then I do this: const newVideoTracks = user.videoTracks;

and I would expect newVideoTracks to be the same as the first image, but instead I get the following:

enter image description here

It has 2 entries and size:2, but it says Map(1) at the top.

I know the console isn't always accurate, but in this case when I subsequently use newVideoTracks, it behaves as though there is only 1 entry.

I assume I need to do some cloning rather than just copying it, so I tried both of the following:

const newVideoTracks = new Map(user.videoTracks); - this didn't make any difference

const newVideoTracks = Object.assign({}, user.videoTracks) - caused a crash

What's the correct way to do this?

Sharon
  • 3,471
  • 13
  • 60
  • 93
  • 1
    How does this map being modified? Is it possible it's getting modified in async? – vanowm Aug 18 '22 at 12:05
  • Yes, I guess so - it's modified by a library, so I don't know the details. – Sharon Aug 18 '22 at 12:06
  • In that case perhaps one of these methods will help cloning the map: https://stackoverflow.com/questions/30626070/shallow-clone-a-map-or-set – vanowm Aug 18 '22 at 12:09

0 Answers0