Iterating over Immutable JS Map
up vote
1
down vote
favorite
Consider this code:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toJS());
// prnts {a: "Hello 1", b: "Hello 2", c: "Hello 3"}
I want the output as ['Hello1', 'Hello2, 'Hello3'], but map is giving me an object with the keys also. Why am I not getting an array as output like a normal JavaScript map works? And what is the best way to achieve the desired result? (I can use forEach instead of map, and push the result of each iteration in an array, but I am looking for a better method).
javascript immutable.js
add a comment |
up vote
1
down vote
favorite
Consider this code:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toJS());
// prnts {a: "Hello 1", b: "Hello 2", c: "Hello 3"}
I want the output as ['Hello1', 'Hello2, 'Hello3'], but map is giving me an object with the keys also. Why am I not getting an array as output like a normal JavaScript map works? And what is the best way to achieve the desired result? (I can use forEach instead of map, and push the result of each iteration in an array, but I am looking for a better method).
javascript immutable.js
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Consider this code:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toJS());
// prnts {a: "Hello 1", b: "Hello 2", c: "Hello 3"}
I want the output as ['Hello1', 'Hello2, 'Hello3'], but map is giving me an object with the keys also. Why am I not getting an array as output like a normal JavaScript map works? And what is the best way to achieve the desired result? (I can use forEach instead of map, and push the result of each iteration in an array, but I am looking for a better method).
javascript immutable.js
Consider this code:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toJS());
// prnts {a: "Hello 1", b: "Hello 2", c: "Hello 3"}
I want the output as ['Hello1', 'Hello2, 'Hello3'], but map is giving me an object with the keys also. Why am I not getting an array as output like a normal JavaScript map works? And what is the best way to achieve the desired result? (I can use forEach instead of map, and push the result of each iteration in an array, but I am looking for a better method).
javascript immutable.js
javascript immutable.js
asked Nov 21 at 10:19
maverick
546723
546723
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
accepted
Use toArray() instead toJS():
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toArray());<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>add a comment |
up vote
0
down vote
You can get the array by simply using myMap.keys
Array.from(myMap.keys());
Runnable code snippet for the same is as under:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(Array.from(myMap.keys()));
add a comment |
up vote
0
down vote
Just providing a different flavor using reduce
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.reduce((accumulator, value) => {
accumulator.push("Hello" + value);
return accumulator;
}, )
console.log(myMap);<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Use toArray() instead toJS():
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toArray());<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>add a comment |
up vote
1
down vote
accepted
Use toArray() instead toJS():
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toArray());<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Use toArray() instead toJS():
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toArray());<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>Use toArray() instead toJS():
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toArray());<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toArray());<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(myMap.toArray());<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>answered Nov 21 at 10:36
Grzesiek Danowski
1905
1905
add a comment |
add a comment |
up vote
0
down vote
You can get the array by simply using myMap.keys
Array.from(myMap.keys());
Runnable code snippet for the same is as under:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(Array.from(myMap.keys()));
add a comment |
up vote
0
down vote
You can get the array by simply using myMap.keys
Array.from(myMap.keys());
Runnable code snippet for the same is as under:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(Array.from(myMap.keys()));
add a comment |
up vote
0
down vote
up vote
0
down vote
You can get the array by simply using myMap.keys
Array.from(myMap.keys());
Runnable code snippet for the same is as under:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(Array.from(myMap.keys()));
You can get the array by simply using myMap.keys
Array.from(myMap.keys());
Runnable code snippet for the same is as under:
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.map((elem, index) => {
return "Hello " + elem;
})
console.log(Array.from(myMap.keys()));
edited Nov 21 at 10:43
answered Nov 21 at 10:24
AurA
9,18853555
9,18853555
add a comment |
add a comment |
up vote
0
down vote
Just providing a different flavor using reduce
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.reduce((accumulator, value) => {
accumulator.push("Hello" + value);
return accumulator;
}, )
console.log(myMap);<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>add a comment |
up vote
0
down vote
Just providing a different flavor using reduce
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.reduce((accumulator, value) => {
accumulator.push("Hello" + value);
return accumulator;
}, )
console.log(myMap);<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>add a comment |
up vote
0
down vote
up vote
0
down vote
Just providing a different flavor using reduce
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.reduce((accumulator, value) => {
accumulator.push("Hello" + value);
return accumulator;
}, )
console.log(myMap);<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>Just providing a different flavor using reduce
const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.reduce((accumulator, value) => {
accumulator.push("Hello" + value);
return accumulator;
}, )
console.log(myMap);<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.reduce((accumulator, value) => {
accumulator.push("Hello" + value);
return accumulator;
}, )
console.log(myMap);<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>const map1 = Immutable.Map({a:1, b:2, c:3});
const myMap = map1.reduce((accumulator, value) => {
accumulator.push("Hello" + value);
return accumulator;
}, )
console.log(myMap);<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>answered Nov 21 at 14:04
jamesjaya
657412
657412
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409871%2fiterating-over-immutable-js-map%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown