Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Copy a Map into a Duplicate Map? [duplicate]

How do I clone/copy a map in JavaScript?

I know how to clone an array but how do I clone/copy a map?

var myArray = new Array(1, 2, 3); var copy    = myArray.slice(); // now I can change myArray[0] = 5; & it wont affect copy array  // Can I just do the same for map? var myMap = new ?? // in javascript is it called a map? var myMap = {"1": 1, "2", 2}; var copy  = myMap.slice();  
like image 568
sazr Avatar asked Nov 21 '11 03:11

sazr


People also ask

Can you duplicate locator maps?

For example, to clone a locator map, they will require another locator map that is empty. Only one clone can be created at a time. Once the player has the same type of map, they have to place the map that needs to be cloned in the first slot of the anvil and the empty map in the second slot.

How do I copy a hash map?

1.1. The best way to create shallow clone of hashmap is to use it's clone() method. It returns a shallow copy of the map. The keys and values themselves are not cloned; and point to same object in memory as in original map. Program output.


Video Answer


1 Answers

With the introduction of Maps in JavaScript it's quite simple considering the constructor accepts an iterable:

var newMap = new Map(existingMap) 

Documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

like image 152
tswaters Avatar answered Sep 16 '22 23:09

tswaters