Array methods in JS

Array methods in JS

Brief overview of some array methods in JS

Firstly, We'll discuss what are arrays in JS. after that, the methods provided by JS.

What are Arrays?

An array in JS is a single variable that can store multiple values in it.

syntax:

declaration variable_name = [item1, item2, item3, .....so on];

The declaration can be var, let and const acc. to the coder.

for ex. If you have a list of cars that may be a list of 3 cars. then, You can store it in maybe in 3 separate variables like-

const car1 = "Hyundai";
const car2 = "Maruti";
const car3 = "volkswagon";

But what happens when you'll have 300 values instead of 3.

You cannot store 300 names in separate variables as we did in the above example. Can you?

The solution for that problem is an array.

Methods Provided by JS to deal with arrays

We'll be discussing the methods which are listed below-

  • filter

  • map

  • find

  • forEach

  • some

  • every

  • reduce

  • include

Filter

The filter() method creates a new array with all the elements that pass the test implemented by the provided function.

for ex.

const number = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.filter(() => numbers % 2 === 0);
console.log(eenNumbers); //output: [2 ,4, 6]

Map

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

for ex.

const numbers = [1, 2, 3, 4, 5, 6];
const doubledNumbers = numbers.map((number) => number * 2);
console.log(doubledNumbers); //output: [2, 4, 6, 8, 10, 12]

Find

The find() method returns the value of the first element in the array that satisfies the providing testing function.

for ex.

const numbers = [1, 2, 3, 4, 5, 6];
const oddNumbers = numbers.find((number) => number % 2 === 1);
console.log(oddNumbers); //output: 1

forEach

The forEach() method executes a provided function once for each element in the array.

for ex.

const numbers = [1, 2, 3, 4, 5, 6];
numbers.forEach((number) => console.log(number));
//output: 1, 2, 3, 4, 5, 6

Some

The some() method tests whether at one element in the array passes the test implemented by the provided function.

for ex.

const numbers = [1, 2, 3, 4, 5, 6];
const hasEvenNumbers = numbers.some((number) => number % 2 === 0);
console.log(hasEvenNumbers); //output: true

Every

The every() method tests whether all the elements in the array pass the test implemented by the provided function.

for ex.

const numbers = [1, 2, 3, 4, 5, 6];
const areAllNumbersEven = numbers.every((number) => number % 2 === 0);
console.log(areAllNumbersEven); //output: false

Reduce

The reduce() method applies a function against an accumulator and each element in the array to reduce it to a single value.

for ex.

const numbers = [1, 2, 3, 4, 5, 6];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(sum); //output: 21

Includes

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

for ex.

const numbers = [1, 2, 3, 4, 5, 6];
const includesNumberThree = numbers.includes(3);
console.log(includesNumberThree); //output: true