5 Array API's in JavaScripts that I ❤️ Most.
Most useful Array API that can same developer time and coding effort.
The filter() method creates a new array with all elements that pass the test implemented by the provided function
- let's say you have an array with several elements and you need to filter out that array based on some parameter/condition.
- In this case, we have an array of numbers that need to filter based on the condition that a given element should be divisible by 3.
- We could just use the filter() API on that array.
As Method suggests if the given elements in the array satisfy the given condition then those elements will be returned from the array.
const arrayOfNumbers = [22, 43, 66, 21, 09, 56];
const divisibleByThree = arrayOfNumbers.filter(item => item % 3 === 0);
console.log(divisibleByThree);
// [ 66, 21, 9 ]
2. Array.map()
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
- let's say you have an array with several elements and now this time instead of filtering the elements you need to perform some operations on the elements and get a new array from the results.
- In this case, we have an array of numbers from that need array with the square of each element.
- We could just use the map() API on that array.
const arrayOfNumbers = [2, 4, 6, 8, 12, 18];
const squaredArray = arrayOfNumbers.map(item => item * item);
console.log(squaredArray);
// [ 4, 16, 36, 64, 144, 324 ]
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
- Many of the naive developers writes lines of code to put an element at the start of the array(I was one of them 🤪).
- No worries unshift() is here to rescue you. It takes the n number of parameters and puts it at the start of the array in the given sequence.
const arrayOfNumbers = [2, 4, 6, 8, 12, 18];
arrayOfNumbers.unshift(0, 1);
console.log(arrayOfNumbers);
// [ 0, 1, 2, 4, 6, 8, 12, 18 ]
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
- This one is my favorite, there are some use cases where you need to check whether any(some) element in an array satisfy the given condition or not.
- some() method returns a boolean value based on the given condition, in the following example we will check if our array contains any even number.
const arrayOfNumbers = [2, 4, 6, 8, 12, 18];
const containsEven = arrayOfNumbers.some(item => item % 2 == 0);
if (containsEven) console.log('arrayOfNumbers contains even numbers');
else console.log('arrayOfNumbers has only odd numbers');
// arrayOfNumbers contains even numbers
There is an similar API that returns true if all the element satisfy the given condition Array.every()
The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
- Many time I faced the issue where I have the recursive array from that I need a single array with up to a specific depth of the element.
- What flat() method does it takes a number(depth) as an argument(default to 1) and then returns the single array recursively up to the given depth.
const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat());
// [0, 1, 2, 3, 4]
const arr2 = [0, 1, 2, [[[3, 4]]]];
console.log(arr2.flat(2));
//[0, 1, 2, [3, 4]]
These are the 5 API that I Love most, there are plenty of APIs in Javascript Array that you can use, you can check the rest of the API's here.