These Are The 7 Must Know JavaScript Array Methods.













Here are few important and regularly used JavaScript array methods which makes things simple while working on arrays.





JavaScript Array Methods



1.forEach():-

forEach() method is used to iterate the entire array. It takes a single argument and a callback function, starts iterating the array elements until the final element. This is used instead of loops because there isn't any need to mention extra things like size of array and other loop variables. Here is an example of forEach() method.



In this example forEach() iterates the array1 array of objects and prints out names of every object in array1.





OUTPUT :-













2.filter():-

filter() method takes a single argument and a callback function, It filters the array with condition implemented by the callback function and passes the filtered elements to a new array. The original array will not be effected. Here is an example of filter() method.









OUTPUT :-

In this example, the new array will be the values of array1 with value > 50.









There are only 3 values which are > 50.





3.map():-

map() method also takes a single argument and a callback function which maps the elements of original array to a new array. It is also similar to filter() method.But map() returns the exact no of elements as of original array but filter() returns the elements according to the condition of callback function.It may or may not update the elements in the original array according to the callback condition. It would be very handy if we want the keys of array of objects. Here is an example of map() method.









OUTPUT :- It creates an array with the values of objects in array1. Here the original array values are not changed.













4.sort():-

sort() method sorts the elements in an array. It takes two arguments.

1st argument --> Any element in the array.

2nd argument --> The next element of the 1st argument.









OUTPUT :- It returns the sorted elements according to the return value.













5.find() :-

find() method takes a single argument and returns the elements if those are found in the array according to the condition.









OUTPUT :- Here find() returns the objects with the name of 'a'.









Here,as there is only one 'a' name it has given object of that.





6.some() :-

some() method takes an argument and returns true or false if at least one element is present according to the condition.

Here is an example of some() method.









OUTPUT :- In the example, if there is at least one value which is < 10 then it returns true.









As there is a value which is < 10 i.e 'value: 2' , it returned true.





7.every():-

every() method is like an antonym of some() method. every() method returns true only if every element in the array met the given condition, otherwise it returns false. Here is an example of every() method.





OUTPUT :- It returns true only if value of every object in the array is < 10.









It returned false as there is only one element which is < 10 in the array of objects.





These are not only methods which makes things easy while coding in JavaScript, there are some other methods too. But these are regularly used methods in a pool of JavaScript methods while working with arrays.



For detailed information about Arrays in JavaScript you can find it on JavaScript Arrays in Developer.Mozilla.Org









HAPPY LEARNING.



