Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
return <div>
{/* I want to do the equivalent of this: */}
{Object.keys(myMap).map(key => <p>{key}: {myMap.get(key)}</p>)}
</div>;
This might be a simple question, but I didn't find how to do that, apart from converting
myMap
to a normal JS object with
toJs()
, which I don't want to do.
Googling didn't help much with map, Map, object... keywords.
according to the map spec and mobx documentation:
https://mobx.js.org/refguide/map.html
you can call the
.keys()
directly on the map:
{myMap.keys().map(key => <p>{key}: {myMap.get(key)}</p>)}
Edit: excuse me i didn't realise iterator doesnt contain map function. You can make your own:
Solution
You can create your own iterator map function:
var x = new Map();
x.set('test','asdf')
console.log(map(x.keys(),(x) => 'prefix:'+x ))
function map(iterator, mapFn){
var result = [];
for(var item of iterator){
result.push(mapFn(item));
return result;
another solution is turing iterator into array:
Array.from(map.keys()).map(...) // array map function of all keys.
hacky prototype method
You can make an extension method in a bit of hacky way, probably does not work for all browsers!:
//Warning this hacky way:
var t = new Map();
t.keys().__proto__.map = function(mapFn){
return map(this, mapFn);
t.keys().map(x=> x) // now usable!
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.