Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Array Induction in JavaScript

Shulou Source: shulou.com Published: 2022-06-03 10:26:31 10月03日 Update

This article will explain in detail the example analysis of array induction in JavaScript. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First, the array induces 1. The string is split into an array console.log (Array.form ("red")) / / ["r", "e", "d"] 2. Convert the collection and mapping into a new array const A1 = new Map (). Set ("name", "Zhang San"). Set ("age", 18) console.log (Array.from (A1)) / [["name", "Zhang San"], ["age", 18] 3. Make a shallow copy of the existing array const A1 = [{name: "Xiaoming", age: 18, gender: "male"}, {name: "Xiaoming", age: 18, gender: "male"}] const a2 = Array.from (A1) console.log (a2)

Running result:

4. The arguments object is easily converted into an array function argumentArray () {console.log (Array.from (arguments)) / / [1,2,3,4]} argumentArray (1,2,3,4) 5. Convert Custom object

Let arrayLike = {0: 'Zhang San', 1:'18', 2: 'male', 3: ['guess', 'where'], 'length': 4} let arr = Array.from (arrayLike); console.log (arr)

Running result:

Array.of (parameter) converts the parameter to an array

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

There are three methods for retrieving arrays on the prototype of Array: keys (), values (), and 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 iterate through the returned array index

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

User.values (), traversing returns array elements

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

User.entries (), traversing returns index / value pairs

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

3. Common operation methods of array

Slice (stratIndex,endIndex)

If the parameters are full, return all the elements from the beginning to the end of the index

If there is only one parameter, returns the corresponding element from the beginning of the index to the end of the index.

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

Used to delete, replace, or insert

Let newData = {"username": "ys", "age": "22", "gender": "Project 1902", "className": "Class 3", "id": 6} person.splice / / here is the use of replacement

This is the end of this article on "sample analysis of array induction in JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Array index parameter Zhang San element method article example analysis object more result Xiaoming run good practical three content prototype only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi macOS Xiaomi Shulou Information