Popular array handling methods (Array) in JavaScript (Part 3)

Tram Ho

Hi guys, in part 1 and part 2 we learned about 15 methods of array.

In this article, I continue to introduce to you the methods that are quite important to the array that devjs often confuse when using the typical slice () with splice ().  Let’s get started

16. slice ()

slice() into Vietnamese is cut, sliced, sliced, bla bla, … and its function is also used to cut off some elements of the array.

slice() returns a shallow copy of a portion of the array as an array that receives index values from begin to end (excluding end).

  • slice() NOT change the original array
  • slice() returns a new array after cutting

With positive index

With negative index

Pretty twisted brain with this yin and yang

Syntax

17. splice ()

splice() used to change elements of an array by deleting an existing element or adding a new element

  • splice() YES changes the original array
  • splice() returns the array containing the deleted elements

This is a rather confusing row when it can both add an element, just delete the element and change it directly on the original array, so if you do not control the arguments passed, it will be opened immediately. See the following example

Delete the element with the start argument

Delete element with additional deleteCount argument (deleteCount: specifies the number of elements to be deleted)

Add the element with the 3rd argument

Add more items

It is not difficult, right, focus is absorbed, sometimes the deadline is about to forget slightly

Syntax

18. Array.from ()

Array.from() used to create a new array of words

  • object is like array (object has length and elements are indexed)
  • iterable objects (objects can get its elements, like Map and Set )

Array.from() returns a new array

Syntax

19. Array.isArray ()

Array.isArray() used to check if the input value is an Array type Array

  • Array.isArray() returns Boolean type: true if array and false if not array

Syntax

summary

Grab the skirt, we have the following summary

MethodFunctionReturn valueChange the original array
concat ()Concatenate 2 or more arraysNew array after joiningIs not
filter ()Filter out the elements that satisfy the conditionNew arrays after filteringIs not
find()Filter out the first element that satisfies the conditionThe value FIRST found / undefinedIs not
forEach ()Browse through each element of the arrayundefined
includes ()Checks whether the element exists in the arraytrue / falseIs not
indexOf ()Finds the position of an element in an arrayIndex FIRST found / -1Is not
join()Create a new stringNew string / That element / ""Is not
map()Create a new array by changing the original arrayNew arrays after transformationIs not
pop()Remove the last element from the arrayThe element has been removed / undefinedHave
shift ()Remove the first element from the arrayThe element has been removed / undefinedHave
push()Add 1 or more elements to the end of the arrayNEW LEVELS of arrayHave
unshift ()Add 1 or more elements to the beginning of the arrayNEW LEVELS of arrayHave
reduce ()Execute the function on each element to get a valueValue after abridgedIs not
reverse ()Reverse the arrayArray after reversingHave
some()Check that at least 1 element satisfies the conditiontrue / falseIs not
sort()Arrange the elements in the arrayArrays after being sortedHave
slice ()Cut off some elements of the arrayNew arrays after cuttingIs not
splice ()Add / remove elements in the arrayThe array contains the deleted elementsHave
Array.from ()Create a new arrayNew array
Array.isArray ()Check if the input value is an arraytrue / false

Conclude

Ok it seems enough, there are some other methods but I find it used so it should not list, if you want to be able to find documents online. Hopefully after reading this series you can master the array, a row will be used a lot with devjs.

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 the following posts!

Share the news now

Source : Viblo