Home > Web Front-end > JS Tutorial > A brief discussion on the method of converting array-like objects into array objects in jquery

A brief discussion on the method of converting array-like objects into array objects in jquery

青灯夜游
Release: 2020-11-19 17:30:22
forward
3983 people have browsed it

A brief discussion on the method of converting array-like objects into array objects in jquery

Related recommendations: "jQuery Video Tutorial"

Definition of array-like objects:

The so-called "array-like object" is a regular Object object, such as $("p"), but it is very similar to an array object: it has a length attribute, and uses numbers such as 0, 1, 2, 3... as attribute names.

But it is not an array after all, and there are no built-in methods inherited from the prototype object of the array (for example: push(), sort(), etc.)

jquer general class array Method of converting an object into an array object

In jquery, you can use the markArray method to convert an array-like object into an array object, such as:

var result = $.makeArray($("p"));
console.log(result instanceof Array);
Copy after login

The result printed by the code at this time is true. In es6, you can use Array.from(...) to achieve array conversion:

Array.from(document.getElementsByTagName("p")
Copy after login

For more programming-related knowledge, please visit: Programming Video Course ! !

The above is the detailed content of A brief discussion on the method of converting array-like objects into array objects in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template