rashmi agar
7 posts
Mar 07, 2025
8:57 PM
|
The array.entries method is a useful tool for iterating over an array while accessing both the index and the value of each element. It returns a new Array Iterator object containing key-value pairs.
Syntax: javascript Copy Edit array.entries() Example Usage: javascript Copy Edit let colors = ["red", "green", "blue"]; let iterator = colors.entries();
for (let [index, value] of iterator) { console.log(index, value); } // Output: // 0 "red" // 1 "green" // 2 "blue" Why Use entries()? Ideal for situations where you need both the index and the value. Works well with destructuring in loops. Useful for iterating over sparse arrays.
|