jQuery.makeArray()S2C Home « Utilities « jQuery.makeArray()

Array object creation.

Description

The jQuery.makeArray() jQuery General utility method, returns a JavaScript Array object created from an array-like object.

Shorthand version $.makeArray()

  • After conversion to an Array object the object is just a plain array with any special features removed.
    1. Generally with array-like objects you can use the array access operator[] and the length property for example.
    2. You do not get full access to the methods of a true Array object.

Syntax

Signature Description
jQuery.makeArray( object )Return an Array object.

Parameters

Parameter Description Type
objectThe object to convert to an Array object.PlainObject

Return

An Array object.

jQuery.makeArray( object ) ExampleTop

Return a JavaScript Array object created from an array-like object.

In the example below when we press the button the first time we create a variable from a node list and test to see if it is an array using the jQuery.isArray() type utility method which returns false.

We then create another variable from the first variable using the jQuery.makeArray( object ) general utility method. We then test this variable against the jQuery.isArray() type utility method which returns true.



$(function(){
  $('#btn1').one('click', function(){
    var pElements = document.getElementsByTagName('p');
    $('#div1').append('Is pElements an Array object? ' +  $.isArray( pElements ) + '<br>');
    var ourArray = jQuery.makeArray(pElements);
    $('#div1').append('Is ourArray an Array object? ' +  $.isArray( ourArray ) + '<br>');
  });
});

div1. Some initial text.

Press the button below to action the above code: