Jump to: navigation, search

JQuery Array Create with push

From w3cyberlearnings

Contents

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 TRY-IT

In this example, we create two arrays: array stores number, and array stores 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

Related Links


Navigation
Web
SQL
MISC
References