Turn a number into an array jquery

jquery convert string to array object
jquery array
jquery array of elements
jquery object to array
jquery string to array
how to create list of objects in jquery
javascript array jquery
jquery convert table row to array

Say for instance I have the number 12:

var number = 12;

How could I change that number into something like:

var n = [0,1,2,3,4,5,6,7,8,9,10,11,12];

Someone probably has a jQuery shortcut, but here's a plain JavaScript solution:

var num = 12;
var n = [];
for (var i=0; i <= num; i++) {

As a function:

function num2Array(num) {
  var n = [];
  for (var i=0; i <= num; i++) {
  return n;

// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

How to convert an integer into an array of digits, I'd go with var arr = n.toString(10).replace(/\D/g, '0').split('').map(Number);. You can omit the replace if you are sure that n has no decimals. Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

I actually had this function sitting around:

function number_range(beginning, end) {
    var numbers = [];
    for (; beginning <= end; beginning++) {
        numbers[numbers.length] = beginning; 
    return numbers;

So if you need to generate more than one of these arrays, it could be useful:

var n = number_range(0, 12);

As for jQuery, well... I don't think that's necessary in this case. (I also don't know of any such function off the top of my head.)

jQuery.makeArray(), makeArray( obj ). obj. Type: PlainObject. Any object to turn into a native Array. Many methods, both in jQuery and in JavaScript in general, return objects that are  While split will work fine if you are sure you have elements in array, if you're expecting data from a server / database you will run into trouble since ''.split(',') has a length === 1 IE: ''.split(',') === [''] – oportocala Aug 2 '16 at 15:49

jQuery doesn't have this, but you could use underscore.js:


_.range([start], stop, [step])

So you could do:

var n = _.range(12);


var n = _.range(0, 12);

.toArray(), Select all divs in the document and return the DOM Elements as an Array; then use the built-in reverse() method to reverse that array. Any 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() ).

Another JavaScript method

var number = 12, i = 0, n = [];
while( n.push( i++ ), i <= number );

.map(), As the return value is a jQuery object, which contains an array, it's very common to call .get() on the result to work with a basic array. The .map() method is  The Number() function converts the object argument to a number that represents the object's value. If the value cannot be converted to a legal number, NaN is returned. Note: If the parameter is a Date object, the Number() function returns the number of milliseconds since midnight January 1, 1970 UTC.

If you need an array just for iterate through you can doing this : Array(12).fill('');

You just need finding the index from a loop fn to retrieve the current number.

How to Convert a JS Object to an Array Using jQuery, How to Convert a JS Object to an Array Using jQuery Answer: Use the jQuery $​.map() Method. You can var array = $.map(myObj, function(value, index){ The split () method is used to split a string into an array of substrings, and returns the new array. Tip: If an empty string ("") is used as the separator, the string is split between each character. Note: The split () method does not change the original string. Browser Support. string .split ( separator, limit) Parameter Values.

How to convert list of elements in an array using jQuery , map() methods ). Take the text value of each child and append that to the array. Example 1: This example uses .each() method to convert a  Converting String to Number. As with the previous shown methods, JavaScript also provides functions to easily transform a String to a primitive number. These are parseInt(), parseFloat(), Math.floor(), Math.ceil(), Unary Operator / Multiply by 1. .parseInt() takes a String as a first argument, and a base to which that String will be converted to.

Array.from(), arrayLike: An array-like or iterable object to convert to an array. mapFn array. thisArg Optional: Value to use as this when executing mapFn . jQuery.inArray ( value, array [, fromIndex ] ) Returns: Number. Description: Search for a specified value within an array and return its index (or -1 if not found). version added: 1.2 jQuery.inArray ( value, array [, fromIndex ] ) The value to search for. An array through which to search. The index of the array at which to begin the search. The

JavaScript Number Methods, Converting Variables to Numbers. There are 3 JavaScript methods that can be used to convert variables to numbers: The Number() method; The parseInt()  Convert String to Array JavaScript. Before we start to convert string to Array in JavaScript using split Method Know about the basics of Split() method. The JavaScript string splitting method returns an array of substrings obtained by splitting a string on the divider you specify. The separator can be a string or a regular expression.

  • I literally need to create a variable like the var n in the question
  • @tfbox The function option doesn't work for you? Added possibly after your comment