1

I just get using Map data structure and I am trying to accomplish the opposite of what is showed on those answers:

How to convert Map keys to array?

Indeed I want achieve same as asked here but the answered proposed does not work:

Indeed:

let answers = [1,"2",3];
let myMap = new Map();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');

myMap.set(answers);

So I would like to get as result:

myMap = ( [1, 'foo'],["2",'bar'],[3,'baz'])

but the new array is just "appended" at the end of the Map as from the https://jsfiddle.net/CarmineT/5m02exn8/, instead I want to substitute each keys( with which rule I don't mind ), so I tried iterate through the iterable ( Map ) such

let k = myMap.keys();

for (let [key, value] of iterable) {
   k.next().value = answers
}

or with a more elegant way, maybe a forEach, but first I would like to understand where I am mistake here as it does not work.

On the similar question, an answer does mention about a library that use the concept of Tree, but I would avoid to use an external library if possible.

Carmine Tambascia
  • 1,628
  • 2
  • 18
  • 32
  • A key needs to have a value. Why would you want to NOT have one? Are you looking for `Set`? – VLAZ May 10 '19 at 20:35
  • @VLAZ maybe I should have been more clear, I want substitute the keys the array values, without touch at all the values. – Carmine Tambascia May 10 '19 at 20:37
  • Are you saying that you have a Map and want to set all available keys to each element in the array? E.g. `{"apple" -> "fruit", "cat" -> "animal"}` when combined with `["a", "b"]` would turn into `{"a" -> "fruit", "b" -> "animal"}`? – VLAZ May 10 '19 at 20:39
  • 1
    please add the wanted result. – Nina Scholz May 10 '19 at 20:40
  • @VLAZ, indeed, I added the wanted result – Carmine Tambascia May 10 '19 at 20:44
  • 1
    `more elegant way, maybe a forEach` IMO: `for of` is more elegant, forEach was elegant, when `const / let & for of` were not available. – Keith May 10 '19 at 20:47

1 Answers1

0

Are you looking for something like this?

var answers = [1,"2",3];
var myMap = new Map();
myMap.set('0', 'foo');
myMap.set(1, 'bar');
myMap.set({}, 'baz');

var newMap = new Map(); 

Array.from(myMap.keys()).forEach((k, i) => {
  const v = myMap.get(k);
  const nk = answers[i];
  newMap.set(nk, v);
});

// print map
for (let [k, v] of newMap){
  console.log(k, " -> ", v)
}
Slawomir Chodnicki
  • 1,525
  • 11
  • 16
  • well actually I did not wanted to create a newMap. I want change in keys of the myMap. I will try your code with the actual myMap. – Carmine Tambascia May 10 '19 at 20:55
  • I would warn against that. If the keys overlap, you will lose values. In fact, your small sample already has a key collision. 'Replacing' key `'0'` with `1` will overwrite value 'bar' prematurely. – Slawomir Chodnicki May 10 '19 at 20:56
  • @ Slawomir Chodnicki you are right. In my example this happens. Unfortunately doing avoiding overlap does add again the actual values with array keys, ending to a Map.size=6. I accepted your answer as for what I need could be a solution create a new Map on the fly. – Carmine Tambascia May 10 '19 at 21:01
  • I don't know what you're trying to achieve with this functionality. But maybe a data structure like `[[k1, v1], [k2, v2], ...]` might suit your purposes as well.... – Slawomir Chodnicki May 10 '19 at 21:04
  • yes that could be also a way. In the end I simplified the logic and created a Map from 2 Arrays. Thanks from your hit, has it helped me to manipulate the Map. Now I have to check 2 values of 2 Maps if they are a "given" couple: "hause"-->"Haus". I am think if better an Object or again a Map, or even better an Array of Objects. – Carmine Tambascia May 11 '19 at 09:07