Array.prototype.entries()

Array.prototype.entries() and Array.prototype.every() methods – Made Easy


ES6 version of JavaScript unwraps a list of array iteration methods that helps its developers write clean and readable code. Included in the list of array iteration methods are Array.prototype.entries() and Array.prototype.every() method which will be explained in this article.

In this article, we will understand the entries() method and the every() method, as well as their use cases.

N/B: An iterator is an object that keeps track of its current position while accessing items in an array one at a time.

According to MDN, an iterator is an object which defines a sequence and potentially a return value upon its termination.

Array.prototype.entries()

The Array.prototype.entries() method returns a new Array Iterator array containing the key/value pairs for each item in the original array. 

Syntax

array.entries();

let ballonDorWinners = [
  {
    name: "Christiano Ronaldo",
    age: 33,
    BallonDor: 6,
    club: "Juventus"
  },
  {
    name: "Messi",
    age: 32,
    BallonDor: 5,
    club: "Barcelona"
  },
  {
    name: "Lucas Modric",
    age: 33,
    BallonDor: 1,
    club: "Real Madrid"
  },
  {
    name: "Ronaldinho ",
    age: 37,
    BallonDor: 3,
    club: "None"
  },
  {
    name: "Ricardo Kaka",
    age: 33,
    BallonDor: 6,
    club: "None"
  }
];
let playersIterator = ballonDorWinners.entries();
for (let player of playersIterator) {
  console.log(player);
}
> Array [0, Object { name: "Christiano Ronaldo", age: 33, BallonDor: 6, club: "Juventus" }]
> Array [1, Object { name: "Messi", age: 32, BallonDor: 5, club: "Barcelona" }]
> Array [2, Object { name: "Lucas Modric", age: 33, BallonDor: 1, club: "Real Madrid" }]
> Array [3, Object { name: "Ronaldinho ", age: 37, BallonDor: 3, club: "None" }]
> Array [4, Object { name: "Ricardo Kaka", age: 33, BallonDor: 6, club: "None" }]

considering the code snippet above and its results, we can see that a new array is created for each item of the original array. The new array contains a key and its corresponding value.

Use case of the Array.prototype.entries() Method 

The entries method is of great importance whenever we want to get the key-value pairs of an array.

Array.prototype.every()

The every() method checks whether all elements in the array pass the test implemented by the callback function. It returns a Boolean value of true if all elements pass the test else it returns a Boolean value of false.

Syntax

myArray.every(callbackFunction);

Callback Function

Syntax

callback(element, index, array)

The callback function houses the conditions to be executed over each element of the array. 


The callback function receives 3 parameters which include:

  • array: the array that is currently being processed. It is optional.
  • index: the index of the current item being processed. It is optional.
  • element: this is the element currently being executed. It is usually required.

The array.every() method in action

Let’s implement a feature to check if the players are due for retirement.

// Add the variable ballonDorWinner from the first code snippet
/* Here */
const shouldRetire = (ballonDorWinner) => ballonDorWinner.age > 40;
console.log(ballonDorWinners.every(shouldRetire));
// expected output: false

Running the code snippet above, the Boolean value of false is logged onto the console because none of the BallonDorWinners age is greater than 40. Moreover, all the array items are expected to pass the test for a Boolean value of true to be returned.Here the shouldRetire function represents the callback function. 

Conclusion

The entries() method returns a new array iterator array containing the key/value pairs for each item in the original array, while the every() method checks whether all elements in the array pass the test implemented by the callback function. The every() method returns a Boolean value of true if all elements pass the test else it returns a Boolean value of false. The entries method is of great importance whenever we want to get the key-value pairs of an array.

Do well to leave your questions or comments in the comment section.


Share on social media

//