jQuery API

jQuery.makeArray()

jQuery.makeArray( obj ) Returns: Array

Description: Convert an array-like object into a true JavaScript array.

  • version added: 1.2jQuery.makeArray( obj )

    objAny object to turn into a native 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: Turn a collection of HTMLElements into an Array of them.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div>First</div>
  <div>Second</div>  
  <div>Third</div>

  <div>Fourth</div>
<script>
    var elems = document.getElementsByTagName("div"); // returns a nodeList
    var arr = jQuery.makeArray(elems);
    arr.reverse(); // use an Array method on list of dom elements
    $(arr).appendTo(document.body);
</script>

</body>
</html>

Demo:

Example: Turn a jQuery object into an array


    var obj = $('li');
    var arr = $.makeArray(obj);

Result:

(typeof obj === 'object' && obj.jquery) === true;
jQuery.isArray(arr) === true;
    

Support and Contributions

Need help with jQuery.makeArray() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.makeArray()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Anonymous

    Can it be use for $.makeArray(arguments)?

  • http://www.learningjquery.com/ Karl Swedberg

    No, but for that you can just do something like this:

    function arrayFromArgs() {
    return Array.slice.call(arguments);
    }

    and then call it:

    arrayFromArgs('3', '2', '4');

  • http://twitter.com/Roguenet Nathan Curtis

    Technically, you can use makeArguments to make the arguments object into a real array, but it's probably slower than the Array.prototype.slice.call() method. The only requirements for makeArguments to turn an object into an array are that the object has a “length” property, and properties with numerical keys, e.g. { 'length':3, '0':”foo”, '1':”bar”, '2':”baz” }. The arguments object satisfies those requirements.

  • Jane

    If theres only I single element I get an empty array instead of an array with single element. Any idea why??

  • Sanshi Ustc

    That's not true, you can use the $.makeArray to transform arguments inside funtion:
    function arrayFromArgs() {
    return $.makeArray(arguments);
    }

    arrayFromArgs('3', '2', '4');

  • Sanshi Ustc

    That's not true, you can use the $.makeArray to transform arguments inside funtion:
    function arrayFromArgs() {
    return $.makeArray(arguments);
    }

    arrayFromArgs('3', '2', '4');

  • Maury M. Marques

    This works.
    $.unique(["a", "b", "c", "d", "a"]);

    I do not know why, but this does not work.
    $.unique(["a", "b", "c", "d", "a", "b"]);