jQuery Create Array From String

Share this article

Simple jQuery code snippet to create an array from a string. Convert your string words into an array using jQuery split() command which is like the PHP explode() method.

var numbersString = "1,2,3,4,5,6";
var numbersArray = numbersString.split(',');
You can then loop through the values of the array like so:
$.each(numbersArray, function(index, value) { 
  alert(index + ': ' + value);
});
You can also join back your array items like so:
var numbersArray = new Array("1", "2", "3", "4", "5", "6");
var numbersString = numbersArray.join(',');
alert(numbersString); 
//alerts 1,2,3,4,5,6
Don’t forget to put your jQuery code inside a document ready function. See 4 different jQuery document ready examples
for more info.

Frequently Asked Questions (FAQs) about jQuery: Creating Arrays from Strings

How can I convert a string into an array using jQuery?

Converting a string into an array in jQuery is quite simple. You can use the JavaScript split() method. This method splits a string into an array of substrings based on a specified separator. Here’s an example:

var str = "Hello World";
var arr = str.split(" ");
In this example, the string “Hello World” is split into an array of two elements: “Hello” and “World”. The space character (” “) is used as the separator.

Can I convert a string into an array without using a separator?

Yes, you can convert a string into an array without specifying a separator. In this case, each character in the string will become an individual element in the array. Here’s how you can do it:

var str = "Hello";
var arr = str.split("");
In this example, the string “Hello” is split into an array of five elements: “H”, “e”, “l”, “l”, and “o”.

How can I convert a comma-separated string into an array?

To convert a comma-separated string into an array, you can use the split() method with a comma (“,”) as the separator. Here’s an example:

var str = "apple,banana,cherry";
var arr = str.split(",");
In this example, the string “apple,banana,cherry” is split into an array of three elements: “apple”, “banana”, and “cherry”.

Can I use jQuery to convert a string into an array?

jQuery is a powerful JavaScript library that simplifies HTML document traversing, event handling, animating, and Ajax interactions. However, it doesn’t provide a specific method to convert a string into an array. You can use the JavaScript split() method for this purpose.

How can I join the elements of an array into a string?

You can use the JavaScript join() method to combine the elements of an array into a string. This method creates a new string by concatenating all the elements of an array, separated by commas or a specified separator string. Here’s an example:

var arr = ["Hello", "World"];
var str = arr.join(" ");
In this example, the array [“Hello”, “World”] is joined into the string “Hello World”. The space character (” “) is used as the separator.

Can I convert a string into an array of integers?

Yes, you can convert a string into an array of integers. However, you need to use the JavaScript map() method to convert each string element into an integer. Here’s an example:

var str = "1,2,3";
var arr = str.split(",").map(Number);
In this example, the string “1,2,3” is split into an array of integers: [1, 2, 3].

How can I convert a string into a multidimensional array?

Converting a string into a multidimensional array involves splitting the string into sub-strings and then splitting each sub-string into an array. Here’s an example:

var str = "1-2-3,4-5-6,7-8-9";
var arr = str.split(",").map(function(item) {
return item.split("-");
});
In this example, the string “1-2-3,4-5-6,7-8-9” is split into a multidimensional array: [[1, 2, 3], [4, 5, 6], [7, 8, 9]].

Can I convert a string into an array of objects?

Yes, you can convert a string into an array of objects. However, this requires a more complex process. Here’s an example:

var str = "name:John,age:30;name:Jane,age:25";
var arr = str.split(";").map(function(item) {
var obj = {};
item.split(",").forEach(function(pair) {
var parts = pair.split(":");
obj[parts[0]] = parts[1];
});
return obj;
});
In this example, the string “name:John,age:30;name:Jane,age:25” is split into an array of objects: [{name: “John”, age: “30”}, {name: “Jane”, age: “25”}].

How can I convert a string into an array using a regular expression?

You can use a regular expression as the separator in the split() method. This allows you to split a string into an array based on a complex pattern. Here’s an example:

var str = "apple banana-cherry";
var arr = str.split(/[\s-]+/);
In this example, the string “apple banana-cherry” is split into an array of three elements: “apple”, “banana”, and “cherry”. The regular expression /[\s-]+/ matches one or more whitespace or hyphen characters.

Can I convert a string into an array in reverse order?

Yes, you can convert a string into an array in reverse order. You can use the split() method to convert the string into an array and then use the reverse() method to reverse the order of the elements. Here’s an example:

var str = "Hello World";
var arr = str.split(" ").reverse();
In this example, the string “Hello World” is split into an array of two elements: “World” and “Hello”.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week