An array of JavaScript

The guide

  1. The array object
  2. How to create an array
  3. The way arrays are deleted
  4. Array query method
  5. Array increment method
  6. Higher-order functions on arrays

The array object

An array is a special type of Object. The type of an array is also Object. Elements of the same data type 2. Use contiguous memory storage 3. Get elements by numeric subscript Elements can have different data types 2. Memory is not necessarily contiguous (objects are stored randomly) 3. The above feature means that the array can have any key value. Some people think that we fetch the array with the number 1.2.3 and so on, but in fact, we fetch the corresponding value of the array with the string

Method to create an array

The usual way to create an array is as follows:

let arr = [1.2.3]; // Create arrays with arrays 1,2, and 3

let arr1 = new Array(1.2.3)// Create arrays with arrays 1,2, and 3

let arr2 = new Array(3// Create an array with three more values

Copy the code

There are a few other ways to convert an array:

let arr = '1, 2, 3'.split(', '// Create an array by separating strings 1,2, and 3 with commas;

let arr1 = '123'.split(' '// Split the string directly into arrays;

Array.from('123'// Try to convert a string to an array, which can also be a pseudo-array (pseudo-number)

There is no array stereotype in the group stereotype chain, that is, an array without common array attributes is a pseudo-array.

Array.from({0:'a'.1:'b'.2:'c'.length:3}); // The method of this conversion is to convert the pseudo-array

Instead of an array

Copy the code

You can also combine two numbers into a new array using the concat method

arr.concat(arr1) // Join the array of ARr1 with the array of arr, return the new array

Copy the code

Removal of an array

arr.slice(3// Get the array after the third arR array

Copy the code

Copy the array

arr.slice(0// Intercepts the entire contents of the array, in other words, copies the array

Copy the code

The way arrays are deleted

delete arr[1// This method can be deleted directly, but only the data with subscript 1 can be deleted without changing the array length

arr.shift() // Delete the uppermost data (changing the array)

arr.pop() // Delete the last data (change the original array)

arr.splice(2.1// Delete a subscript 2 (delete itself), return the delete value

arr.splice(2.1.'x'// Delete the current index and add x to the array

Copy the code

Array query method

Ojbect.keys(arr); // Check all key values

Ojbect.values(arr); // Query all key values

arr.forEach(function(item,index{console.log(`{index}: {item}`)}) 

// View the value of the array property name

Copy the code

In arrays, the subscript value starts at 0, so when querying arrays with arr.length as subscript, the value is undefined

Arr. IndexOf (number)// Query the arR array for the index of the value. If no arR array is found, -1 is returned

arr.find(x= > x%5= = =0// If the value is true once, only the first occurrence value is returned, otherwise undefined

Copy the code

Array increment method

arr.push() // Add elements to the end of the array

arr.unshift() // Add elements to the header of the array

arr.splice(index,0.'x'// Insert the 'x' element at index

arr.splice(index,0.'x'.'y'// Insert multiple elements at index.

Copy the code

There is also an API for reversing the order of arrays as follows:

arr.reverse() // This reverses the array order

Copy the code

You can use sort to sort arrays, but only in the Unicode-coded sort order, which requires passing a function into the method to sort properly

function compare (x,y{

  if(x >y) {

  return 1

  } else if (x === y) {

  return 0

  } else {

  return - 1




Copy the code

Higher-order functions on arrays

/ / the map function > item*item) // arr is an array. This function iterates through each item in the array, multiplies itself, and returns a new array

/ / filter function

arr.filter(item= > item % 2= = =0// arr is an array. This function iterates through each item in the array and compares the conditions. If true is returned, a new array containing the element that returns true is returned. This method makes n arrays smaller

/ / the reduce function

arr.reduce((result,item) = > {return result + item},0//arr is an array. This method iterates through each item in the array, sets an initial value, which in this case is 0, adds them up one by one, and returns a value

Copy the code

The three functions mentioned above have their own uses, which are more useful in the actual combat of the project. If readers see the need to traverse the situation, they can consider the use of these three functions


The blogger has summarized 4 articles about JavaScript for readers’ reference, among which there are a lot of content not involved, such as prototype, variable promotion,call,apply and some other content. In the future blog, if the blogger has practical application scenarios, he will provide reference for everyone. Thanks for reading this article. Remember to keep learning and follow up! Come on!