jQuery.makeArray( obj )
Returns: Array
Description: Convert an array-like object into a true JavaScript array.
Many methods, both in jQuery and in JavaScript in general, return objects that are array-like. For example, the jQuery factory function $() returns a jQuery object that has many of the properties of an array (a length, the [] array access operator, etc.), but is not exactly the same as an array and lacks some of an array's built-in methods (such as .pop() and .reverse()).
Note that after the conversion, any special features the object had (such as the jQuery methods in our example) will no longer be present. The object is now a plain array.
Examples
Example 1
Turn a collection of HTML Elements into an Array of them.
var arr = jQuery.makeArray(document.getElementsByTagName("div"));
arr.reverse(); // use an Array method on list of dom elements
$(arr).appendTo(document.body);
Example 1 - Full source:
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function(){ var arr = jQuery.makeArray(document.getElementsByTagName("div")); arr.reverse(); // use an Array method on list of dom elements $(arr).appendTo(document.body); }); </script> <style> div { color:red; } </style> </head> <body> <div>First</div> <div>Second</div> <div>Third</div> <div>Fourth</div> </body> </html>
Example 2
Turn a jQuery object into an array
var obj = $('li');
var arr = $.makeArray(obj);
(typeof obj === 'object' && obj.jquery) === true;
jQuery.isArray(arr) === true;
jQuery.isArray(arr) === true;
Was this information helpful?

