Hi Folks,
Let's begin with some useful Javascript Array methods which are commonly used in day to day life of a JS developer.
Hope you will enjoy it.
1. join() — creates a string from an array of items means it will join your array with a separator and outputs it to a string.
2. indexOf — it will find an item’s index. (If element is present it will prints its index otherwise it will return -1)
3. Concat() — It will concatenate one or more arrays. (Easy way to concatenate arrays).
4. Slice() — It will split an array at a given index. And most importantly, it will not change the original array, the original array remains intact.
5. Splice() — It will also split an array and we can insert or add new items. The only difference with Slice() is — Original array will be changed/mutates.
6. forEach()- it will loop over an array and access each element/item.
7. map() — it will also loop over an array and run some operation on each item without mutating/changing the original array.
8. Filter() — Creates a new array based on filter criteria. It will not mutate/change the original array.
9. Reduce() — It will run a callback/function on each item and reduce the array to a single accumulated value.
10. flat() — New method — used to flatten an array to a single 1-d dimension. basically, it flattens an array to a single level.
11. sort() — It will sort an array.
12. find() — it will find the first value that meets a condition.
13. findIndex() — Find the index of an item based on a condition.
14. every() — check if every item meets a condition.
15. some() — check if some values meet a condition
16. entries() — This method returns a new Array Iterator object that contains the key/value pairs for each index in the array. (It works like a generator fn but it is not generator function)
use for … of loop with index and elements
using for… of loop
17. includes() — This method determines whether an array includes a certain value among its entries, returning
true
orfalse
as appropriate.
18. Keys & Values — Array is like an object ( so it has keys and values methods also)
The
keys()
method returns a new Array Iterator object that contains the keys for each index in the array.The
values()
method returns a new array iterator object that contains the values for each index in the array.
But wait,
There are more in the list, which can be used for your project, application, solving complex problems, algorithms, etc.
For more updates, You can refer to the MDN Docs for more details ( Also check some cool methods at(), groupBy(), groupByToMap(), etc that are still in experimental stage)
Hope you liked it, please like, subscribe and share and also save it for future reference. :)