ITZone

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

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

Method Function Return value Change the original array
concat () Concatenate 2 or more arrays New array after joining Is not
filter () Filter out the elements that satisfy the condition New arrays after filtering Is not
find() Filter out the first element that satisfies the condition The value FIRST found / undefined Is not
forEach () Browse through each element of the array undefined
includes () Checks whether the element exists in the array true / false Is not
indexOf () Finds the position of an element in an array Index FIRST found / -1 Is not
join() Create a new string New string / That element / "" Is not
map() Create a new array by changing the original array New arrays after transformation Is not
pop() Remove the last element from the array The element has been removed / undefined Have
shift () Remove the first element from the array The element has been removed / undefined Have
push() Add 1 or more elements to the end of the array NEW LEVELS of array Have
unshift () Add 1 or more elements to the beginning of the array NEW LEVELS of array Have
reduce () Execute the function on each element to get a value Value after abridged Is not
reverse () Reverse the array Array after reversing Have
some() Check that at least 1 element satisfies the condition true / false Is not
sort() Arrange the elements in the array Arrays after being sorted Have
slice () Cut off some elements of the array New arrays after cutting Is not
splice () Add / remove elements in the array The array contains the deleted elements Have
Array.from () Create a new array New array
Array.isArray () Check if the input value is an array true / 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