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

The latest version of MySQL5.7.19 decompression version installation guide

MySQL version: MySQL Community Edition (GPL) ----...

Summary of various methods of MySQL data recovery

Table of contents 1. Introduction 2. Direct recov...

Design theory: people-oriented green design

Reflections on the two viewpoints of “people-orie...

A detailed discussion of evaluation strategies in JavaScript

Table of contents A chestnut to cover it Paramete...

Implementation of Nginx operation response header information

Prerequisite: You need to compile the ngx_http_he...

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...

MySQL 8.0.20 installation and configuration method graphic tutorial

MySQL download and installation (version 8.0.20) ...

HTML table tag tutorial (26): cell tag

The attributes of the <TD> tag are used to ...

The best way to start a jar package project under Centos7 server

Preface Everyone knows how to run a jar package o...

Summary of common docker commands (recommended)

1. Summary: In general, they can be divided into ...

Pure js to achieve typewriter effect

This article example shares the specific code of ...

Class in front-end JavaScript

Table of contents 1. Class 1.1 constructor() 1.2 ...