DEV Community

Pritom Deb
Pritom Deb

Posted on • Updated on

Important array methods

1. concat()
Joins two or more arrays, and returns a copy of the joined arrays

the num will return [1, 2, 3, 4, 5, 6]

2. every((element, index, array) => { /* ... */ })
Checks if every element in an array pass a test
Image description
teenager will reply true while adult while reply false. The every() method only returns true or false. The every() method does not execute the function for empty elements. Every element has to pass the test for the every() method to work.

3. filter((element, index, array) => { /* ... */ })
Creates a new array with every element in an array that pass a test
Image description
adult will return a new array ["18","19","20"]

4. find((element, index, array) => { /* ... */ })
Returns the value of the first element in an array that pass a test
Image description
adult will return 18.

5. findIndex((element, index, array) => { /* ... */ })
Returns the index of the first element in an array that pass a test
Image description
adult will return 3. Will return -1 if no element matches the condition.

6. forEach((element, index, array) => { /* ... */ })
Calls a function for each array element
Image description
adult will return undefined. It is very important to note that forEach only iterate through a array. It doesn't return any value.

7. Array.form(arrayLike, (element, index) => { /* ... */ })
Creates an array from an object
Image description
nameArray will return ["p", "r", "i", "t", "o", "m"]

8. includes(searchElement, fromIndex)
Check if an array contains the specified element
Image description
adult will return true.

9. indexOf(searchElement, fromIndex)
Search the array for an element and returns its position
Image description
adult will return 3. Will return -1 if no element matches the condition.

10. lastIndexOf(searchElement, fromIndex)
Search the array for an element, starting at the end, and returns its position
Image description
adult will return 4. Will return -1 if no element matches the condition.

11. map((element, index, array) => { /* ... */ })
Creates a new array with the result of calling a function for each array element
Image description
everyone will return [14, 15, 16, 17, 18, 19, 20]

12. pop()
Removes the last element of an array, and returns that element
Image description
newAges will return 20. pop() returns the element it removed.

13. push(element0, element1, /* ... ,*/ elementN)
Removes the last element of an array, and returns that element
Image description
newAges will return 8. The push() method adds new items to the end of an array. The push() method returns the new length.

14. reduce((previousValue, currentValue, currentIndex, array) => { /* ... */ }, initialValue)
Reduce the values of an array to a single value (going left-to-right)
Image description
total will return 10. The reduce() method returns a single value: the function's accumulated result.

15. reduceRight((previousValue, currentValue, currentIndex, array) => { /* ... */ }, initialValue)
Reduce the values of an array to a single value (going right-to-left)

Image description
total will return 10. The reduceRight() method returns a single value: the function's accumulated result.

16. reverse()
Reverses the order of the elements in an array
Image description
newAges and ages both will return [4, 3, 2, 1]. The reverse() method overwrites the original array.

17. shift()
Removes the first element of an array, and returns that element
Image description
newAges will return 1. The shift() method returns the shifted element.

18. slice(start, end)
Selects a part of an array, and returns the new array
Image description
newAges will return [1, 2]. The slice() method returns selected elements in an array, as a new array.

19. some((element, index, array) => { /* ... */ } )
Checks if any of the elements in an array pass a test
Image description
newAges will return true. The some() method only returns true or false. Only one element has to pass the test for some() method to work.

20. splice(start, deleteCount, item1, item2, itemN )
Adds/Removes elements from an array
Image description
newAges will return [15, 16, 17]. ages will return [18, 19, 20]. The splice() method overwrites the original array.

21. toString()
Converts an array to a string, and returns the result
Image description
newAges will return "1,2,3,4,5". The toString() method returns a string with array values separated by commas.

22. unshift(element0, element1, /* ... ,*/ elementN)
Adds new elements to the beginning of an array, and returns the new length
Image description
newAges will return 6. The unshift() method overwrites the original array.

Top comments (1)

Collapse
 
leonardoromero profile image
Leo Romero

Very useful!