Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Array Induction in JavaScript

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report