Array handling methods are common in JavaScript

Tram Ho

Hello everyone, many tips on CSS and today we have changed a bit with JavaScript. The topic today I want to share is “Array handling methods (Array) popular in JavaScript”. These methods are sure you will use a lot when working with JavaScript and its frameworks.

This sharing part helps new students to master their knowledge, partly also to revise their knowledge (mainly for me to have a source to search for when the code is cool.  we begin

1. concat ()

concat() used to concatenate 2 or more arrays

  • concat() NOT change the original array
  • concat() returns a new array after joining

Syntax

2. filter ()

filter() used to filter out elements in the array that meet certain conditions

  • filter() NOT change the original array
  • filter() returns a new array after filtering
  • filter() returns an array THAT if no element meets the condition

Syntax

3. find ()

find() also used to filter elements in the array, but it will return the FIRST value found in the array or may return undefined if not found.

  • find() NOT change the original array

Syntax

4. forEach ()

forEach() used to browse each element of the array

  • forEach() returns undefined

Syntax

5. includes ()

This is a new method in ES6

includes() checks whether the specified element exists in the array

  • includes() NOT change the original array
  • includes() returns Boolean type: true if found or false if not found

Syntax

6. indexOf ()

indexOf() used to search for the position of an element in an array

  • indexOf() NOT change the original array
  • indexOf() returns the FIRST index value of the array if the element exists in the array
  • indexOf() returns -1 if the element does not exist in the array

Syntax

7. join ()

join() used to create a new string by concatenating all array elements, by default separating them with a comma or a specified string of characters .

  • join() NOT change the original array
  • join() returns the element itself if the array has only one element
  • join() returns an "" empty string "" if arr.length === 0

Syntax

8. map ()

map() helps to create a new array with elements resulting from executing a function on each element.

  • map() NOT change the original array

Syntax

9. pop ()

pop() used to remove the last element from the array

  • pop() YES changes the original array
  • pop() returns the element that was removed from the array
  • pop() returns undefined if the array is empty

Syntax

10. shift ()

In contrast to pop() is shift()

shift() used to delete the first element from the array

  • shift() YES changes the original array
  • shift() returns the deleted element from the array
  • shift() returns undefined if the array is empty

Syntax

Conclusion

The article seems to be long enough, I would like to pause here to avoid feeling bored when the surrounding is all text

Well, there is a small note that why does each method indicate what method is returning? Does it change the original array? You should pay close attention, because later when working with data will be very important to these issues, when you control the data well, the output of the method will know exactly when to use which method to request. sometimes not as clammy as me

If you find a good article, give me +1 upvote. If you like me, click the follow button for more cool stuff. Good luck !

See you in part 2!

Share the news now

Source : Viblo