A must to know javascript array methods for developers

Must know JavaScript Array methods for developers

In this blog-post I will share some of Javascript array methods which I find useful for everyone who writes or learn JavaScript and so i consider them as a must to know array methods in javascript.

Am assuming you already familiar with what JavaScript array is and what is method in JavaScript.

 

1. forEach()

The foreach method will help you loop over items of an array. Its faster and more efficient way of iterating over array elements in javascript without using traditional forloop statement.

Example:

const sophia_grades = [80,90,79,88,87]

sophia_grades.forEach(grade =>{

console.log(grade);

});

 

2. map()

This method will return new array by calling specified function to every element of the array.

Its useful when you want to manipulate every element in an array and then get new array with manipulated elements.

Example:

const data = [4,6,9,3,4,9]

console.log(data)

If you want to get new array from the data array above by multiplying each element by a specific value then you can use the map method.

const new_data = data.map(el => el * 2);

console.log(new_data)

so the new_data array now is the new array whose element are product of 2 of elements from array data

 

3. filter()

Ever wanted to get rid of something in your life? Well the filter method of an array can do a related thing in array. It create new array from existing array by taking only elements which pass a particular test.

const data = Array.of(88,99,33,55,23)

const filtered_array = data.filter(el => el >33)

console.log(filtered_array)

4. some()

This method check if array at least pass one test, and return True otherwise return false, Let say you want to see if array contain any number which is less than 4 or if an array has any element whose length is greater than five.

Example:

const data = [1,8,9,0,9,4,5,2]

Check if an array contain any data whose value is 9

const results = data.some(el => el ==9)

console.log(results)

const resultsTwo = data.some(el =>el==15)

console.log(resultsTwo)

 

5. every()

This method check if all element of an array pass a  particular test.

Example:

const sophia_grades = [80,90,79,88,87]

To check if all sophia grades are greator than 70  all we have to do is to use the every() array method.

const results = sophia_grades.every(grade => grade >70)

console.log(results)

const results2 = sophia_grades.every(grade => grade > 95)

console.log(results2)

6. sort()

At some point we might want to sort JavaScript array. And this is where sort() method come in.

Example:

const sophia_grades = [80,90,79,88,87]

To sort sophia grades in ascending order

const results_one = sophia_grades.sort((v1,v2) => v1 > v2 ? 1 : -1);

console.log(results_one)

To sort sophia grades in descending order

const results_two = sophia_grades.sort((v1,v2) => v1 > v2 ? -1 : 1);

console.log(results_two)

 

7. includes()

This method return true if array contain the specified element.

In our previous example we used sophia grades array example, We  can use this method. To check if sophia has 79 in her grades 

Example:

const sophia_grades = [80,90,79,88,87]

sophia_grades.includes(79) ; // output true

sophia_grades.includes(100); //output false

 

8. find()

This method will return the first element which match a particular query.

const data = Array.of(88,99,33,55,23)

const el = data.find( v => { return v>88} );

console.log(el)

Note:  Array.of()  is just another way of creating array in javascript.

Have we forgotten any useful javascript array method you already know? Please let us know on the comment section bellow. tag @bongobinary. Dont forget to like, follow and subscribe to our social accounts.

 

Conclusion:


There are many other JavaScript array methods available, those highlighted here are the one which you are mostly going to use in Javascript. I believe this article has been useful to you please don't forget to follow bongobinary on Facebook, Instagram and twitter. @bongobinary . Till next time happy coding.