Array Method

MASUM ALAMMASUM ALAM
3 min read

What is array ?

a collection of data that’s stored various types of data type which can be accessible.

The array is non-primitive data type in the manner, in this blog we can define the array method as listed below :

✌️Let’s with array methods :

1 . some()

“some“ method in array help to get each element value is equivalent with the checker.

const array = [1,2,3,4];
const even = (e) => e % 2 === 0;
console.log(array.some(even)); // returning true

2 . sort()

array sort method which help sorting the element of inside the array

const weeks = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

weeks.short();
console.log(weeks);

3. map()

array map method help pupolate each element with the help of fallback function. map method is very usefull when need to extract data from an array.

const NameOfarray = ["Masum", "Aasim", "Sameer", "Other"];

const mapResult = NameOfarray.map((element) => element);

console.log(mapResult);

4. slice()

This is taken two type of argument, first one is start value where from take the include the value and another on e is taken end point

const fruits = ["Lemon", "Orange", "", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);

4. pop()

This is array method to help to get remove last from a existing array. Let‘s get an example

const arrayNum = [1, 2, 3, 5, 6];
arrayNum.pop();
console.log(arrayNum.pop());

5. reduce()

The reduce() method is used to reduce an array to a single value by executing a function on each element of the array, one by one.

const arrayFruits = ["Apple", "Banana", "Guava", "Orange"];
const arrayNum = [1, 2, 3, 5, 6];
const total = arrayNum.reduce((a, b) => a + b, 0);
console.log(total);

6. filter()

The filter array method, It’s help create shallow copy of array, filtered down to just the elements from the given array that pass the test implemented by provide function.

const fruits = ["Apple", "Orange", "Banana", "Guava", "Lemon"];

const resultFruit = fruits.filter((fruit) => fruit.length > 6);

console.log(resultFruit);

7. join()

The join method that help to join one to another array.

const arrayFirst = ["Ma", "su", "m"];
console.log(arrayFirst.join()); //Masum

8. concat()

This array help to merge one or more array in sinlge array. The Array concat() method ":

let odds = [1, 3, 5];
let evens = [2, 4, 6];

let results = odds.concat(evens);
console.log(results);

9. reduceRight()

The reduceRight() method reduces an array to a single value by applying a callback from right to left. If no initial value is given, the last element is used as the starting accumulator. Also works like a join() mthod.

const words = ["Hii", "I", "Am", "Masum"];
const sentence = words.reduceRight((acc, curr) => acc + " " + curr);
console.log(sentence);
0
Subscribe to my newsletter

Read articles from MASUM ALAM directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

MASUM ALAM
MASUM ALAM