I was trying to swap keys with values in Map and Object.
In Map it goes in an infinite loop, but in Object it goes in a finite loop. I don’t know why is that happening. Please help!
Here is the Code.
const obj = { 1: "one", 2: "two" };
const map = new Map([
[1, "one"],
[2, "two"],
]);
for (const [key, value] of map) { // Goes Infinite Loop
map.set(value, key);
map.delete(key);
}
console.log(map); // you will never get this response
for (const [key, value] of Object.entries(obj)) { // Goes Finite
obj[value] = key
delete obj[key];
}
console.log(obj); // you will get the response