From the docs I’ve been reading, the following ought to create an Array
from the keys of a Map
:
var m = new Map();
m.set("a", 1);
m.set("b", 2);
var arr1 = Array.from(m.keys());
var arr2 = Array.from(m.values());
console.log(arr1.length);
console.log(arr2.length);
I’d expect that to print 2
for both arrays, but in reality both arrays are empty and it prints 0
. What gives?
Edit: This issue seems to occur on a specific webpage when I execute the above code in the Javascript console. If I execute it on other pages it works as expected.
Edit 2: Upon further investigation, it appears that the page in question is including prototype.js v1.6.1 which is somehow causing Array.from
to break.
2
Answers
This appears to be caused by a known bug in
prototype.js
. See this GitHub issue "Native Array.from is overwritten by $A which is not able to handle all cases used by the native function #338".An excerpt from the issue:
In your case
prototype.js
is overriding or interfering with the nativeArray.from
method, since because of that it malfunctions. This can result inArray.from
not working as expected and not delivering the result expected, leading to empty arrays when trying to convert the keys and values of the Map object.To avoid this issue and as a work around use
Array Spread
operator.Using spread operator :
Using slice: