JQuery Array Create with push

From CoderLearner
Jump to: navigation, search

jQuery Create Array

Create Array in jQuery uses push() function.

Syntax $.push()

  • myFruits is the array
  • newarray is the new element to append to the array
 
myFruits.push(newarray);

Check Integer

parseInt(myIds[i]);

Check None Integer

isNaN(parseInt(myIds[i]));

Example

In this example, we create two arrays: array stores only number and the array stores only string elements.

$(document).ready(function() {
	var myIds =["app","app2","pp2",3,40];

	var onlyNumber = [];
	var onlyString = [];

	for(var i=0; i < myIds.length; i++)
	{
		if(parseInt(myIds[i])){
			onlyNumber.push(myIds[i]);
		}
		else if(isNaN(parseInt(myIds[i])))
		{
			onlyString.push(myIds[i]);	
		}
	}

	// display number 
	var onlyNumber2str=onlyNumber.join('-');
	alert(onlyNumber2str);
				
	// display string			
	var onlyString2str=onlyString.join('-');
	alert(onlyString2str);
});

Output

3-40  
app-app2-pp2