Must Know JavaScript Array Methods (Cheat Sheet)

    Hey There, Here are a few important and regularly used JavaScript array methods which make things simple while working on arrays. In this article, we will see some must know javascript array methods.

Must Know JavaScript Array Methods (Cheat Sheet)


So, If you want to know some important javascript array methods, Stay tuned! If we are done wasting time.

Let's just keep all the things aside and Let the tech begin!


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 the size of array and other loop variables. Here is an example of the forEach() method.

Must Know JavaScript Array Methods (Cheat Sheet)


Output

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

Must Know JavaScript Array Methods (Cheat Sheet)


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 affected. Here is an example of the filter() method.

Must Know JavaScript Array Methods (Cheat Sheet)


Output

In this example, the new array will be the values of array1 with a value > 50. There are only 3 values which are > 50.

Must Know JavaScript Array Methods (Cheat Sheet)


3. map()

map() method also takes a single argument and a callback function which maps the elements of the original array to a new array. It is also similar to the filter() method.But map() returns the exact no of elements as of the original array but filter() returns the elements according to the condition of a 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 an array of objects. Here is an example of the map() method.

Must Know JavaScript Array Methods (Cheat Sheet)


Output

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

Must Know JavaScript Array Methods (Cheat Sheet)


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.

Must Know JavaScript Array Methods (Cheat Sheet)


Output

It returns the sorted elements according to the return value.

Must Know JavaScript Array Methods (Cheat Sheet)


5. find()

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

Must Know JavaScript Array Methods (Cheat Sheet)


Output

find() returns the objects with the name of 'a'. Here, as there is only one 'a' name it has given object of that.

Must Know JavaScript Array Methods (Cheat Sheet)


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.

Must Know JavaScript Array Methods (Cheat Sheet)


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.

Must Know JavaScript Array Methods (Cheat Sheet)


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 the every() method.

Must Know JavaScript Array Methods (Cheat Sheet)


Output

It returns true only if the 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.

Must Know JavaScript Array Methods (Cheat Sheet)


Conclusion

These are not only methods that make 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.

So, That was all about some important JavaScript Array Methods. Keep supporting Techniverse Spotted for more cool and interesting stuff.

Post a Comment

Previous Post Next Post