Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the map key/value pair I’ve in the JavaScript “map”:

var map = {"a": 1, "b": 2, "c": 3};

alert(JSON.stringify(map));

I want to get an mapper which contains key/value pair on each iteration like below: 

// ["a_1", "b_2", "c_3"]

map.map((key, value) => key + "_" + value);

Can anyone tell me how to do it?

 

1 Answer

0 votes
by (19.7k points)

You can use Object.entries to map on the key value pair like below:  

const map = {"a": 1, "b": 2, "c": 3};

const mapped = Object.entries(map).map(([k,v]) => `${k}_${v}`);

console.log(mapped);

Object.entries gives the following output: 

[

  [ "a", 1 ],

  [ "b", 2 ],

  [ "c", 3 ]

]

To create the String with template literals, you have to loop through every inner array. 

Interested in Java? Check out this Java tutorial by Intellipaat.  

Related questions

0 votes
1 answer
asked Jun 20, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...