Home  >  Article  >  Web Front-end  >  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

青灯夜游
青灯夜游forward
2020-11-19 17:28:573939browse

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);

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")

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete