Here are some important JavaScript Array methods you need to know ππΌ
-
map(callbackFn):-
mapapplies a provided function (callback) to each element in an array and returns a new array with the results. - Example:
const fruits = ['π', 'π', 'π', 'π']; const result = fruits.map((fruit) => fruit + 'π'); // Result: ['ππ', 'ππ', 'ππ', 'ππ']In this example, the callback function concatenates 'π' to each fruit.
-
-
filter(callbackFn):-
filtercreates a new array containing all elements that pass a provided test (callback). - Example:
const numbers = [1, 2, 3, 4, 5]; const result = numbers.filter((number) => number % 2 === 0); // Result: [2, 4]The callback function checks if a number is even and includes it in the result array.
-
-
find(callbackFn):-
findreturns the first element in the array that satisfies the provided test (callback). - Example:
const fruits = ['π', 'π', 'π', 'π']; const result = fruits.find((fruit) => fruit === 'π'); // Result: 'π'The callback function searches for the first occurrence of 'π' in the array.
-
-
findIndex(callbackFn):-
findIndexreturns the index of the first element in the array that satisfies the provided test (callback). - Example:
const fruits = ['π', 'π', 'π', 'π']; const result = fruits.findIndex((fruit) => fruit === 'π'); // Result: 3The callback function finds the index of the first 'π' in the array.
-
-
fill(value, start):-
fillchanges all elements in an array to a static value (value) from a starting index (start). - Example:
const numbers = ['π','π','π','π']; numbers.fill('π',1); // Result: ['π', 'π', 'π', 'π']It fills all elements from index 1 onward with the value π.
-
-
some(callbackFn):-
somechecks if at least one element in the array satisfies the provided test (callback) and returns a Boolean. - Example:
const numbers = [1, 2, 3, 4, 5]; const result = numbers.some((number) => number > 3); // Result: trueThe callback function checks if at least one number is greater than 3.
-
-
every(callbackFn):-
everychecks if all elements in the array satisfy the provided test (callback) and returns a Boolean. - Example:
const numbers = [1, 2, 3, 4, 5]; const result = numbers.every((number) => number > 3); // Result: falseThe callback function checks if all numbers are greater than 3 (which is not true).
-
-
reduce(callbackFn, initialValue):-
reduceapplies a function (callback) against an accumulator and each element in the array to reduce it to a single value. - Example:
const numbers = [1, 2, 3, 4, 5]; const result = numbers.reduce((accumulator, number) => accumulator + number, 0); // Result: 15The callback function accumulates the sum of all numbers with an initial value of 0.
-
-
includes(searchElement):-
includeschecks if an array includes a certain element (searchElement) and returns a Boolean. - Example:
const colors = ['π£', 'π£', 'π‘', 'π‘']; const result = colors.includes('π£'); // Result: trueIt checks if 'π£' is present in the array.
-
-
indexOf(searchElement):-
indexOfreturns the first index at which a given element (searchElement) is found in the array, or -1 if it's not present. - Example:
const colors = ['π£', 'π£', 'π‘', 'π‘']; const result = colors.indexOf('π‘'); // Result: 2It finds the first occurrence of 'π‘' at index 2 in the array.
-
You find this blog helpful. Please like and share with others.
Top comments (0)