console.log(item))”."> How to convert pseudo array to array in ES6-Front-end Q&A-php.cn

How to convert pseudo array to array in ES6

青灯夜游
Release: 2022-05-05 14:04:20
Original
1861 people have browsed it

In ES6, you can use the from() method of the array type to convert a pseudo array into an array. This method can convert an array-like object or a traversable object into a real array. The syntax "Array. from(pseudo array object).forEache(item=>console.log(item))".

How to convert pseudo array to array in ES6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

ECMAScript6 new features - pseudo array

What is a pseudo array: If all the keys of an object are positive integers or zero, and there is a length attribute, then this object Just like an array, it is called a pseudo array.

Typical pseudo-arrays: arguments objects, most DOM element sets, and strings.

Example

let arrayLike = { "0": "a", "1": "b", "2": "c", "length": 3 }
Copy after login

Like the arrayLike object above, it has a length attribute and the key is also an ordered sequence.

So you can traverse and query the length. But you cannot call array methods. Such as push, pop and other methods.

Before ES6, there was also a common pseudo-array: arguments.

arguments also looks like an array, but it does not have array methods.

For example, arguments.push(1), this will definitely report an error.

How to convert a pseudo array into an array in ES6

In ES6, you can use the from method of the Array type to convert a pseudo array into an array .

The Array.from() method is used to convert two types of objects into real arrays:

1. Array-like objects can be understood as "pseudo arrays"

2. Traversable objects (such as strings)

 


Copy after login

The main function of Array.from is to convert pseudo arrays and traversable objects into arrays.

The reason why we say "main function" is because Array.from also provides 2 parameters to pass. This can extend many kinds of small gameplay.

  • The second parameter of Array.from is a function, similar to the map traversal method. Used to traverse.

  • The third parameter of Array.from accepts a this object, which is used to change the this pointer.

Usage of the third parameter (not commonly used)

let helper = { diff: 1, add (value) { return value + this.diff; // 注意这里有个 this } }; function translate () { return Array.from(arguments, helper.add, helper); } let numbers = translate(1, 2, 3); console.log(numbers); // 2, 3, 4
Copy after login

Expand knowledge: convert string into array

let msg = 'hello'; let msgArr = Array.from(msg); console.log(msgArr); // 输出: ["h", "e", "l", "l", "o"]
Copy after login

[Related recommendations:javascript video tutorialweb front-end

The above is the detailed content of How to convert pseudo array to array in ES6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!