Array keys() & values() Method

Introduction

The `keys()` and `values()` methods are built-in JavaScript array methods that allow you to extract the keys and values of the elements in an array. These methods work in a similar way to the `Object.keys()` and `Object.values()` methods, but operate specifically on arrays.

The `keys()` Method

  • The `keys()` method returns an iterator object that contains the keys(indices) of the elements in the array.
  • This iterator object can be looped through using the `for...of` loop or converted to an array using the `Array.from()` method.

  • Syntax: array.keys()
  • Mutable: No (Arrays are not mutable due to this method)

Here's an example:

const fruits = ['apple', 'banana', 'orange'];
 
const keys = fruits.keys();
 
for (const key of keys) {
  console.log(key);
}

Output:

0
1
2

In the above example, the `keys()` method returns an iterator object containing the keys of the `fruits` array. The `for...of` loop is then used to iterate over the keys and log each key to the console. In this case, the keys are the indices of the elements in the array.

Applications of Array.keys():

  • It is useful when you specifically need to work with the indices of array elements in a loop or when you want to do something with the array elements based on their index.
  • It can be combined with other array methods like Array.values() or Array.entries() to get the indices, values, or key-value pairs of the array elements and perform different operations.

The `values()` Method

  • The `values()` method returns an iterator object that contains the values of the elements in the array.
  • This iterator object can be looped through using the `for...of` loop or converted to an array using the `Array.from()` method.

  • Syntax: array.values()
  • Mutable: No (Arrays are not mutable due to this method)

Here's an example:

Example 1:

const values = fruits.values();
 
for (const value of values) {
  console.log(value);
}
// Output:
// apple
// banana
// orange

In the above example, the `values()` method returns an iterator object containing the values of the `fruits` array. The `for...of` loop is then used to iterate over the values and log each value to the console.

Example 2:

// Input array contain some elements
let A = ['A', 'Z', 'k', 'B'];
 
// Here array.values() method is called.
let valuesIterator = A.values();
 
// All the elements of the array the array
// is being printed.
console.log(valuesIterator.next().value);
console.log(valuesIterator.next().value);
console.log(valuesIterator.next().value);
console.log(valuesIterator.next().value);
 
// Output:
// A
// z
// k
// B

valuesIterator.next().value meaning

In JavaScript, valuesIterator.next().value is a sequence of method calls used to retrieve the next value from an iterator object. The valuesIterator is an iterator returned by the Array.values() method (or any other method that returns an iterator).

Let's break down the meaning of each part of valuesIterator.next().value:

  1. valuesIterator: It is an iterator object obtained by calling the Array.values() method on an array. The valuesIterator is capable of iterating through the values of the array elements one by one.
  2. next(): It is a method called on the iterator object valuesIterator. The next() method is used to move the iterator to the next element in the iteration sequence. When called, it returns an object with two properties: value and done.
  3. value: It is a property of the object returned by valuesIterator.next(). It represents the current value of the iterator after moving to the next element in the array. This is the value of the current array element in the iteration sequence.

Here's an example to illustrate the usage of valuesIterator.next().value:

const fruits = ['apple', 'banana', 'orange'];
 
const valuesIterator = fruits.values();
 
const firstValue = valuesIterator.next().value;
console.log(firstValue); // Output: 'apple'
 
const secondValue = valuesIterator.next().value;
console.log(secondValue); // Output: 'banana'
 
const thirdValue = valuesIterator.next().value;
console.log(thirdValue); // Output: 'orange'

Applications of Array.values():

  • It is useful when you specifically need to work with the values of array elements in a loop or when you want to perform operations based on the values of the array elements.
  • It can be combined with other array methods like Array.keys() or Array.entries() to get the indices, values, or key-value pairs of the array elements and perform different operations.

Key Takeaways

  • The `keys()` method returns an iterator object containing the keys of the elements in an array.

  • The `values()` method returns an iterator object containing the values of the elements in an array.

  • These methods can be useful when you specifically need to work with the keys or values of the elements in an array.

Comments

Popular posts from this blog

Limitations of JavaScript

'for in' Loop

in Operator