could someone explain to me how exactly map.entries() and destructuring work?
var numbers = new Map()
numbers.set(1,'one')
numbers.set(2,'two')
numbers.set(3,'three')
numbers.set(4,'four')
var [key,value] = numbers.entries() // returns key as [1,'one'] and value as [2,'two']
//but when using for..of loop
for(let [key,value] of numbers.entries()){
console.log(key,value)
} // it works and console logs keys and values
why does this work in a for..loop? and how does the .entries() method work exactly?i've been looking all over the web and couldn't really understand it.
[key, value]
assignment is misleading. You're not getting a key and a value there. It would be better named[firstKeyValuePair, secondKeyValuePair]
, but I doubt this is even what you want. – Spirillum.entries()
in thefor .. of
loop because the map is directly iterable. – Fractureentries
did return an array, you'd be getting the same result. – Licence