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

Detailed explanation of incompatible changes in rendering functions in Vue3

Table of contents Rendering API changes Render fu...

Write a shopping mall card coupon using CSS in three steps

Today is 618, and all major shopping malls are ho...

Detailed explanation of JavaScript axios installation and packaging case

1. Download the axios plugin cnpm install axios -...

How to switch directories efficiently in Linux

When it comes to switching directories under Linu...

In-depth understanding of asynchronous waiting in Javascript

In this article, we’ll explore how async/await is...

Two common solutions to html text overflow display ellipsis characters

Method 1: Use CSS overflow omission to solve The ...

React error boundary component processing

This is the content of React 16. It is not the la...

Sample code for implementing DIV suspension with pure CSS (fixed position)

The DIV floating effect (fixed position) is imple...

Introduction to Nginx log management

Nginx log description Through access logs, you ca...

HTML table tag tutorial (7): background color attribute BGCOLOR

The background color of the table can be set thro...

Three common methods for HTML pages to automatically jump after 3 seconds

In practice, we often encounter a problem: how to...

How to deploy the crownblog project to Alibaba Cloud using docker

Front-end project packaging Find .env.production ...