Posts

Showing posts with the label JavaScript Array

Array reduce() Method

Introduction The reduce() method in JavaScript is used to reduce an array to a single value by executing a callback function on each element of the array. The callback function takes in four parameters: the accumulator, the current value, the current index, and the array itself. The reduce() method iterates over the array from left to right and accumulates the values returned by the callback function. Syntax The syntax of the `reduce()` method is as follows: array. reduce ( callback ( accumulator , currentValue , currentIndex , array ) , initialValue ) Parameters: callback:  The function to execute on each element of the array. It takes in the accumulator, the current value, the current index, and the array itself. accumulator:  It accumulates the return values of the callback function. currentValue:  The current element being passed from the array. currentIndex (optional):  The index of the current element being processed. array (optional):  The array on wh...

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...

Array some() & every() Method

Introduction The `some()` and `every()` methods are built-in JavaScript array methods that help to check if the elements of an array meet certain criteria. These methods provide a straightforward and concise way to perform common tasks such as checking if at least one element in an array satisfies a condition or if all elements satisfy a condition. 1. some() Method: Checks if at least one element in the array satisfies the provided testing function. It returns true if there is at least one matching element, otherwise false. Syntax: array.some(callbackFunction(element, index, array)) Mutable: No Parameters of the callback function: element: The current element being processed in the array. index (Optional): The index of the current element being processed in the array. array (Optional): The array on which some() was called. Example: const numbers = [ 1 , 4 , 7 , 10 , 15 ] ;   const hasEvenNumber = numbers. some ( number => number % 2 === 0 ) ;   console. log ( ...

Array find() & findIndex() Method

JS find() & findIndex() Method The `find()` and `findIndex()` methods are array methods in JavaScript that allow you to search for a specific element in an array based on a given condition. 1. The `find()` Method Returns the value of the first element in the array that satisfies the provided testing function. Otherwise, it returns undefined. Syntax: array.find(callbackFunction(element, index, array)) Mutable: No Parameters of the callback function: element: The current element being processed in the array. index (Optional): The index of the current element being processed in the array. array (Optional): The array on which find() was called. Example: const numbers = [ 1 , 4 , 7 , 10 , 15 ] ;   const foundNumber = numbers. find ( number => number > 5 ) ;   console. log ( foundNumber ) ; // Output: 7 In this example, the find() method searches for the first element in the numbers array that is greater than 5 and returns that element. It stops the search as ...

Array Iteration Methods

Array Iteration Methods Array iteration methods are built-in methods in JavaScript that allow us to perform specific actions on each element of an array. These methods provide a more concise and readable way to operate on arrays compared to traditional for loops. There are several iteration methods available in JavaScript, including `for...of`, `forEach`, `map`, `filter`, and `reduce`. In this guide, we will explore each of these methods in detail. The `for...of` Loop The `for...of` loop is a newer addition to JavaScript that provides a simple way to iterate over the elements of an array. It allows you to execute a block of code for each element in the array. Here's an example of using the `for...of` loop to iterate over an array: const numbers = [ 1 , 2 , 3 , 4 , 5 ] ;   for ( const number of numbers ) { console. log ( number ) ; } //Outpt: // 1 // 2 // 3 // 4 // 5 In this example, the `for...of` loop iterates over each element of the `numbers` array and logs i...