JQuery Array Create with split

From CoderLearner
Jump to: navigation, search

jQuery Create Array

Create Array in jQuery uses split() function.

Syntax $.split()

  • myFruits is the string
  • w_sep is the separator
 
myFruits.split(w_sep);

Example 1

Split the string by the space separator and return the array elements.

$(document).ready(function() {
	var myFruits='Apple Banana Coconut Mango Pear Orange';
	var myFruitsArray = myFruits.split(' ');
				
	alert(myFruitsArray[0]);
});

Output

Apple

Example 2

Split the string by the 'xxx' separator and return the array lists.

	
$(document).ready(function() {
	var myFruits='ApplexxxBananaxxxCoconutxxxMangoxxxPearxxxOrange';
	var myFruitsArray = myFruits.split('xxx');
				
	alert(myFruitsArray[2]);
});

Output

Coconut