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

HTML+CSS+JavaScript to create a simple tic-tac-toe game

Table of contents Implementing HTML Add CSS Imple...

Detailed description of the function of meta name="" content="

1. Grammar: <meta name="name" content...

20 JavaScript tips to help you improve development efficiency

Table of contents 1. Declare and initialize array...

Interpretation of syslogd and syslog.conf files under Linux

1: Introduction to syslog.conf For different type...

Solve nginx "504 Gateway Time-out" error

Students who make websites often find that some n...

Overview of the definition of HTC components after IE5.0

Before the release of Microsoft IE 5.0, the bigges...

Details on using order by in MySQL

Table of contents 1. Introduction 2. Main text 2....

Mysql transaction concurrency problem solution

I encountered such a problem during development A...

Solution to using html2canvas to process Dom elements with Baidu map into images

Problem 1: Baidu Map uses tiled images (the map i...

Example analysis of mysql stored procedure usage

This article describes the usage of MySQL stored ...

How to connect Django 2.2 to MySQL database

1. The error information reported when running th...

Several ways to implement "text overflow truncation and omission" with pure CSS

In our daily development work, text overflow, tru...