JS: Array.prototype.entries
New in JS2015.
arrayX.entries()
-
Return a Iterator (also is a Iterable Object), each yield is an array of the form
[index, value]
.
for (let e of ["a", "b", "c"].entries()) { console.log( e ); } // prints // [ 0, 'a' ] // [ 1, 'b' ] // [ 2, 'c' ]
for (let [i,k] of ["a", "b", "c"].entries()) { console.log( i, k ); } // prints // 0 'a' // 1 'b' // 2 'c'
Convert array to Map with index:
〔see Map Object〕
// convert array to map const aa = ["a", "b", "c"]; const mm = new Map([...aa.entries()]); console.log( mm ); // Map { 0 => 'a', 1 => 'b', 2 => 'c' }
〔see Spread Operator〕
This example shows the result is iterator and iterable:
const et = ["a", "b", "c"].entries(); // is a iterator console.log( Object.prototype.toString .call (et) === "[object Array Iterator]" ); // true // is also a iterable console.log( Symbol.iterator in et ); // true
JS Array.prototype
- JS: Array.prototype.length
- JS: Array.prototype.constructor
- JS: Array.prototype.values
- JS: Array.prototype.pop
- JS: Array.prototype.push
- JS: Array.prototype.shift
- JS: Array.prototype.unshift
- JS: Array.prototype.splice
- JS: Array.prototype.fill
- JS: Array.prototype.copyWithin
- JS: Array.prototype.concat
- JS: Array.prototype.slice
- JS: Array.prototype.entries
- JS: Array.prototype.keys
- JS: Array.prototype.values
- JS: Array.prototype.map
- JS: Array.prototype.forEach
- JS: Array.prototype.filter
- JS: Array.prototype.sort
- JS: Array.prototype.reverse
- JS: Array.prototype.includes
- JS: Array.prototype.indexOf
- JS: Array.prototype.lastIndexOf
- JS: Array.prototype.find
- JS: Array.prototype.findIndex
- JS: Array.prototype.some
- JS: Array.prototype.every
- JS: Array.prototype.reduce
- JS: Array.prototype.reduceRight
- JS: Array.prototype.join
- JS: Array.prototype.toString
- JS: Array.prototype.flat (Flatten Array)
- JS: Array.prototype.flatMap