I have a map that looks like (it's not constant and there could be more subarrays)
var mMp= new Map([
[ "A", [1, "r"] ],
[ "B", [2, "d "]]
]);
I am trying to get to:
var ks = "A, B"
var vs1 = "1, 2"
var vs2 = "r, d"
I could do
mMp.forEach( (val, key) => {.....};
where I add the stuff manually, but I'd have to check for the last value. I also thought about
Array.from(mMp.keys()).join(", ");
but that also would not help with the values, i guess. And it's less efficient, although I'm not too worried about efficiency here.
What's the best way I can get those 3 sets for strings? Thank you!
Array.from
is indeed the way to go. For the values, use .values()
instead of .keys()
and the callback:
var mMp = new Map([
["A", [1, "r"]],
["B", [2, "d"]],
// …
]);
var ks = Array.from(mMp.keys()).join(", "); // "A, B"
var vs1 = Array.from(mMp.values(), v => v[0]).join(", "); // "1, 2"
var vs2 = Array.from(mMp.values(), v => v[1]).join(", "); // "r, d"
If you have value arrays of arbitrary lengths, use a loop to create all your vs
arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With