Summary of basic usage of js array

Summary of basic usage of js array

Preface

Arrays are a special kind of object. There is no real array in js, it just uses objects to simulate arrays.

The same method for viewing object properties also applies to arrays. It is worth noting that the array subscript type is a string, not a number.

The difference between typical arrays and js arrays

Typical array features similar to C/C++ are as follows:

  • The data types of the elements are the same
  • Use contiguous memory storage
  • Get elements by numeric subscript

js array

  • The data types of the elements can be different
  • Memory is not necessarily contiguous (objects are stored randomly)
  • It cannot be accessed through numeric subscripts, but through string subscripts. (This means that the array can have any key)

The above figure illustrates the last point: an array can have any key. To prove that the subscript of an array is a string, use Object.keys(arr)

Creating an Array

There are two ways to create an array:

 let arr = [1,2,3]
 let arr = new Array(1,2,3)

Convert string to array split() Array.from()

There are two ways to convert a string into a string array.

Pseudo-array

An array without common attributes of an array is a pseudo-array (there is no prototype of an array in the prototype chain of a pseudo-array)

There are no push, pop and other methods in the pseudo array (as can be seen by console.dir(divList) ). We can convert it through Array.from()

After the conversion, you can push successfully.

Merge two arrays

  • concat()

This method does not change the original array.

 let arr1 = [1,2,3]
 let arr2 = [4,5,6]
 arr1.concat(arr2) // [1,2,3,4,5,6]
 arr1//[1,2,3]
 arr2//[4,5,6]

Intercepting an Array

  • slice()

This method does not change the original array.

let arr = [1,2,3,4,5,6]
arr.slice(3) //[4,5,6]
arr//[1,2,3,4,5,6]

Deleting array elements

Delete the head element: arr.shift() arr will be modified and the deleted element will be returned

 let arr = [1,2,3,4,5,6]
 arr.shift() // 1
 arr//[2, 3, 4, 5, 6]

Delete the tail element: arr.pop() arr will be modified and the deleted element will be returned

 let arr = [1,2,3,4,5,6]
 arr.shift() // 6
 arr//[1, 2, 3, 4, 5]

Delete the middle:

arr.splice(index,1) //Delete the first element of index and return the deleted element arr.splice(index,1,'x') //Add 'x' at the deleted position and return the deleted element arr.splice(index,1,'x','y') //Add 'x' and 'y' at the deleted position and return the deleted element

 let arr = [1,2,3,4,5,6,7,8,9]
 //Delete element 4
 arr.splice(3,1)//4
 arr//[1, 2, 3, 5, 6, 7, 8, 9]
 
 let arr = [1, 2, 3, 5, 6, 7, 8, 9]
 //Delete subscript 3 and add 3.5 and 4
 arr.splice(2,1,3.5,4) // 3
 arr//[1, 2, 3.5, 4, 6, 7, 8, 9]

View array elements

View Properties

  • Object.keys(arr)
  • Object.values(arr)
let arr = [1,2,3,4,5]
arr.x='xxx'
Object.keys(arr) //["0", "1", "2", "3", "4", "x"]
Object.values(arr) // [1, 2, 3, 4, 5, "xxx"]

for in loop

View elements containing only numbers

for循環

forEach循環

Check if an element is in the array

arr.indexOf(item) returns the array index if it exists, otherwise it returns -1

let arr = [1,2,3,4,5,6]
arr.indexOf(2) // 1
arr.indexOf(7) // 0

Finding elements using conditions

//Find the first even element let arr=[1,2,3,4,5,6]
arr.find(item=>item%2===0)//2

//Find the first even subscript let arr=[1,2,3,4,5,6]
arr.findIndex(item=>item%2===0)//1

Add elements to an array

Add at the end: arr.push(item1,item2)

 let arr = [3,4,5,6]
 arr.push(7,8,9)
 arr//[3,4,5,6,7,8,9]

Header addition: arr.unshift(item1,item2)

 let arr = [3,4,5,6]
 arr.unshift(1,2,3)
 arr//[1, 2, 3, 3, 4, 5, 6]

Add in the middle: arr.splice(index,0,'x')

 let arr = [1,2,3,4,5,6,7]
 //Add 3.33,3.44 to the position with subscript 2
 arr.splice(2,0,3.33,3.44) // [1, 2, 3.33, 3.44, 3, 4, 5, 6, 7]

Summarize

This concludes this article on the basic usage summary of js arrays. For more relevant js array usage content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Commonly used JavaScript array methods
  • JavaScript Array Detailed Summary
  • In-depth understanding of javascript class array
  • Summary of JavaScript array simplification techniques
  • A brief introduction to JavaScript arrays

<<:  MySQL 5.7.17 winx64 installation and configuration method graphic tutorial

>>:  How to configure MySQL on Ubuntu 16.04 server and enable remote connection

Recommend

IE6 BUG and fix is ​​a preventive strategy

Original article: Ultimate IE6 Cheatsheet: How To...

About the usage and precautions of promise in javascript (recommended)

1. Promise description Promise is a standard buil...

ES6 loop and iterable object examples

This article will examine the ES6 for ... of loop...

Docker builds cluster MongoDB implementation steps

Preface Due to the needs of the company's bus...

Nginx/Httpd reverse proxy tomcat configuration tutorial

In the previous blog, we learned about the usage ...

JavaScript explains the encapsulation and use of slow-motion animation

Implementing process analysis (1) How to call rep...

Common commands for deploying influxdb and mongo using docker

Deploy database based on docker sudo docker pull ...

Installation and use tutorial of Elasticsearch tool cerebro

Cerebro is an evolution of the Elasticsearch Kopf...

Implementation of dynamic rem for mobile layout

Dynamic rem 1. First, let’s introduce the current...

Example of ellipsis when CSS multi-line text overflows

Ellipses appear when multi-line text overflows Th...