Learn about arrays in JavaScript

Tram Ho

Introduce

Today I will introduce you a data type in Javascript, which is Array . It will help you be able to represent complex data and make it easier to manage and process the data

So what is Array?

Arrays , or arrays , are data types whose values ​​contain many other values. Each value of an array is called an element .

There are 2 ways to declare Array in Javascript:

  • Using Array literals – Represented by square brackets and followed by empty values ​​or one or more values ​​separated by commas

  • Using Array constructor – Created using the keyword ‘new’

Properties

length — The property that specifies the length of the array. If you use a negative integer, a real number or a string as an index, the length of the array is also the highest positive integer.

For example:

Method

There are many methods related to arrays. Some methods are used to change the original array ( Mutator Methods ), some are used to access values, return some necessary values ​​and do not change the existing array ( Accessor Methods ), methods used. to iterate the elements in the array ( Iterator Methods ), … And now we will learn some of the methods above.

Mutator Methods

  • Array.prototype.push() – Method to add an element to the end of the array and return the length of the array.

  • Array.prototype.pop() – Method to delete the last element of the array and return the deleted element.

  • Array.prototype.shift() – Like pop , this method removes the first element of the array and returns that element.

  • Array.prototype.splice() – A method used to insert or delete one or more elements. splice can have multiple number changes, the first is the starting index, the second is the number of elements to be deleted from the first argument (if zero, there is no deleting element), followed by possible arguments or no, if so, they will be but the element is inserted immediately after the starting position.

Accessor Methods

  • Array.prototype.includes() – Returns true or false , checking for the existence of elements in the array.

  • Array.prototype.indexOf() – Returns the first index found. If not found returns -1 . It also takes the 2nd argument (optional) as the starting position.

  • Array.prototype.join() – A method of concatenating array elements into a string. It takes the argument that is the connection point between the elements.

Iteration methods

  • Array.prototype.filter() – Repeats elements in the given array and returns a new array with elements that satisfy the filter condition. The following example uses the ‘arrow function’ :

  • Array.prototype.map() – Create a new array with the change of elements in the array.

  • Array.prototype.find() – Returns the first element in the array that matches the search criteria.

  • Array.prototype.reduce() – A more complex method than the other methods. It browses through the elements in the array and invokes a callback on each element, the values ​​returned from one callback to another, and finally returns the value after browsing to the end of the array.

map() , filter() , find() , reduce() are very powerful methods for searching, transforming and processing data. They make your code easy to understand, not lengthy, easier to maintain.

Conclude

There are many methods and properties of arrays that I have not yet been able to mention. Hopefully the above article will help you getting started with arrays in Javascript more accessible.

Reference : Array In JavaScript

Share the news now

Source : Viblo