I'm preparing a Map and doing some operations

var tempParentMap = new Map();

tempParentMap[key] = someList; This code works when I iterate through forEach loop Like:

Object.keys(tempParentMap).forEach(function(key) {
   //Do Something

But when I'm doing this- tempParentMap.set(key, someList); The Map doesn't let me iterate through. The for loop is breaking and doesn't go inside. Also I noticed that when I use set method of Map it gives me Array of Map. It gives me something like below:

[[Entries]]: Array(3)
0: {"01t1b0000018M1uAAE" => Proxy}
1: {"CardProcessing" => Proxy}
2: {"01t1b0000018M2EAAU" => Proxy}

The Iterations which are failing is as below:

for(let key in tempParentMap){ // Doen't go inside, it breaks and exit the loop. I'm guessing this is because, the `tempParentMap` is an Array of Map.
    //Do Something.
//Same happens with below code as well
Object.keys(tempParentMap).forEach(function(key) {
   //Do Something

Just trying to understand whats happening here? If anyone would be able to explain that would be great!


Try one of the following as your for loop:

for(let key of tempParentMap.keys()){ 


tempParentMap.forEach(function(value, key) {
   console.log(key + ’-’ + value);
  • The second one seems to be working but the first one is failing in the same way as it is failing for others. But still not clear why this is happening? – SFDC Mafia - VIC May 21 '19 at 10:24
  • @SFDCMafia-VIC try to change in the first one from ’in’ to ’of’ as I've just edited. – Derminal May 21 '19 at 10:30
  • @SFDCMafia-VIC does it work? – Derminal May 22 '19 at 4:31
  • Partially, Now it is iterating and going inside the for loop, but the value is null and the key has value. I just want to understand why this is happening, do you have any insights? – SFDC Mafia - VIC May 22 '19 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.