-2
map = new Map<number, string>();
this.map.set(1, "educadmin");
this.map.set(6, "hr");
this.map.set(7, "tech");
console.log(map);
//process 1
for (let entry of this.map.entries()) {
    console.log(entry[0], entry[1]);  
}
//process 2
Array.from(map.values(), (value: string) => {
      console.log(value);
});

I get this when I logged to the console:

Map(0) {}
size: (...) 
__proto__: Map
[[Entries]]: Array(3)
  0:{6 => "hr"}
  1: {1 => "educadmin"}
  2: {7 => "tech"}
length: 3

I want to add get all these values -> hr , educadmin and tech to a string[] I tried above 2 process but I am not able to read

  • 1
    And your question is...? – Jared Smith Nov 16 '18 at 18:45
  • I am not able to read the values – Rajesh Rajoji Nov 16 '18 at 18:46
  • https://github.com/Microsoft/TypeScript/issues/6842 – Rajesh Rajoji Nov 16 '18 at 18:48
  • What do you mean? What did you try? What values? You wrote `map = new Map(); this.map.set(1,"rajesh");` but the one logged in the console has different entries. What relevance does the github issue have? There's no `for..of` in your code. None of this makes any sense. – Jared Smith Nov 16 '18 at 18:48
  • https://howtodoinjava.com/typescript/maps/ – Rajesh Rajoji Nov 16 '18 at 18:49
  • I tried all these ways from the links commented above. but no luck – Rajesh Rajoji Nov 16 '18 at 18:49
  • 3
    No luck with what? I'm voting to close this as unclear. *Post the code you've tried*, along with the desired output and error messages (if any), and I'll consider retracting my close vote. Also not clear at all what this has to do with typescript. – Jared Smith Nov 16 '18 at 18:51
  • map = new Map(); I did u get it? – Rajesh Rajoji Nov 16 '18 at 18:54
  • I want to read the values from the map. please help me to read them – Rajesh Rajoji Nov 16 '18 at 18:56
  • 2
    Read what values? Again, please edit the question with a [minimal, complete, verifiable example](https://stackoverflow.com/help/mcve). Everything you've posted is disjointed and in some cases contradictory. Reading values out of a map is a simple as calling `someMap.get(key)`, if it's not working something else is going on but you haven't posted enough for me to say. Your edit made it worse, not better. – Jared Smith Nov 16 '18 at 18:57
  • Please look the question , i edited now – Rajesh Rajoji Nov 16 '18 at 19:20
  • 1
    That's a *huge* step in the right direction. Now edit the output that you get from running your code into the question and why it isn't correct, because `Array.from(map.values())` gives me `["hr", "educadmin", "tech"]`. – Jared Smith Nov 16 '18 at 19:25

1 Answers1

0

I think you are looking for a solution to get the keys/values as arrays.

let keysAsArray = Array.from(map.keys());
let valueAsArray = Array.from(map.values());

console.log(keysAsArray);
console.log(valueAsArray);

The output is

[1, 6, 7]
["educadmin", "hr", "tech"]
  • Yeah. I tried this way. But it does it work. I’m getting empty array. My typescript Version is 2.9 and using ecma 5. Do you feel that your code still supports these versions. – Rajesh Rajoji Nov 17 '18 at 01:03
  • 1
    `Map` is introduced in ES6. You need to use polyfil. Can you take a look at [this](https://stackoverflow.com/questions/40427777/es6-map-doesnt-compile-to-es5-when-using-typescript), [this](https://stackoverflow.com/questions/30019542/es6-map-in-typescript), and [this](https://stackoverflow.com/questions/51043439/using-latest-javascript-features-in-typescript-such-as-es2018/51044240#51044240) to get more insights and eventually to solve your issue ? – Charmis Varghese Nov 17 '18 at 22:14