JavaScript Array Detailed Summary

JavaScript Array Detailed Summary

1. Array Induction

1. Split a string into an array

 console.log(Array.form("red")) // ["r", "e", "d"] 

2. Convert collections and maps to new arrays

 const a1 = new Map().set("name","张三").set("age",18)

    console.log(Array.from(a1)) // [["name","张三"], ["age",18] 

3. Make a shallow copy of an existing array

 const a1 = [

        {

        name: "Xiao Ming",

        age: 18,

        gender: "male"

        },

        {

            name: "Xiao Ming",

            age: 18,

            gender: "male"

        }]

    const a2 = Array.from(a1)

    console.log(a2) 

Running results:

4. The arguments object can be easily converted to an array

 function argumentArray() {

        console.log(Array.from(arguments)) // [1, 2, 3, 4]

    }

    argumentArray(1, 2, 3, 4) 



5. Convert custom objects

let arrayLike = {

        0: 'Zhang San',

        1: '18',

        2: 'Male',

        3: ['Guess', 'Which one'],

        'length': 4

    }

    let arr = Array.from(arrayLike);

    console.log(arr); 



Running results:

Array.of(參數) will convert the parameter to an array

 Array.of(1, 2, 3, 4) // [1, 2, 3, 4] 

2. Iterator Method

There are three methods on the Array prototype for retrieving arrays: keys() , values() , entries()

 Array.of(1, 2, 3, 4) // [1, 2, 3, 4] 


 let user = [

        {

            name: "Zhang San",

            age: 18,

            gender: "male"

        },

        {

            name: "Li Si",

            age: 19,

            gender: "female"

        },

        {

            name: "Wang Wu",

            age: 20,

            gender: "female"

        }

    ] 



First use user.key(), to traverse the returned array index

 console.log(Array.from(user.keys())) // [0, 1, 2] 

user.values(), traverse and return array elements

 console.log(Array.from(user.values())) 

user.entries(), traverse and return index/value pairs

console.log(Array.from(user.entries())) 

3. Common array operations

slice(stratIndex,endIndex)

  • If the parameters are full, return all elements from the start index to the end index;
  • If there is only one parameter, return the corresponding elements from the start index to the end index.

splice(startIndex, length, new1, new2....)

  • To delete, replace or insert
 let newData = {"username": "ys","age": "22","gender":"Ji Ke 1902","className":"Class 3","id":6}

    person.splice(1,1,newData) // This is where the replacement is used 

This is the end of this article on the detailed summary of JavaScript arrays. For more relevant JavaScript array content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

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

<<:  How to modify the MySQL character set

>>:  Zabbix WEB monitoring implementation process diagram

Recommend

Detailed explanation of the specific use of the ENV instruction in Dockerfile

1. The ENV instruction in the Dockerfile is used ...

Detailed introduction to nobody user and nologin in Unix/Linux system

What is the nobody user in Unix/Linux systems? 1....

Introduction to Sublime Text 2, a web front-end tool

Sublime Text 2 is a lightweight, simple, efficien...

How to configure VMware multi-node environment

This tutorial uses CentOS 7 64-bit. Allocate 2GB ...

MySQL database rename fast and safe method (3 kinds)

Table of contents How to rename MySQL database Th...

Form submission refresh page does not jump source code design

1. Design source code Copy code The code is as fol...

JavaScript to achieve custom scroll bar effect

In actual projects, the up and down scroll bars a...

How to implement parallel downloading of large files in JavaScript

Table of contents 1. HTTP Range Request 1.1 Range...

Detailed explanation of html download function

The new project has basically come to an end. It ...

How to use CSS custom variables in Vue

Table of contents The CSS custom variable functio...

A brief introduction to React

Table of contents 1. CDN introduction 1.1 react (...