Author Archives: super-admin

How to sort two dimensional associative array in javascript ?

var testArray = [];

testArray[0] = { 'countryCode':'af', 'countryName':'Afghanistan' };
testArray[1] = { 'countryCode':'np', 'countryName':'Nepal' };
testArray[2] = { 'countryCode':'in', 'countryName':'India' };
testArray[3] = { 'countryCode':'dz', 'countryName':'Algeria' };

//Function to do sort on two dimensional array------------------------------------------------------------------------
function by(i,dir) {
	return function(a,b){
		a = a[i];
		b = b[i];
		try{
			if(typeof a !=="undefined" && typeof b !=="undefined"){
				if(isNaN(a) && isNaN(b)){
					return a.toLowerCase() == b.toLowerCase() ? 0 : (a.toLowerCase() < b.toLowerCase() ? -1*dir : dir);
				}else{
					return a == b ? 0 : (a < b ? -1*dir : dir);
				}
			}
		}catch(err){
			document.write(err.description);
		}
	}
}

//Function call --------------------------------------------------------------------------------------------
testArray.sort(by("countryCode",1)); //Sort by countryCode with Ascending order
/*Following loop will result
af - Afghanistan
dz - Algeria
in - India
np - Nepal
*/
for(i=0,count=testArray.length;i<count;i++){
	document.write(testArray[i]['countryCode'] + " - " + testArray[i]['countryName'] + "<br>");
}

document.write("-----------------------------------------------------------------------------------<br>");

testArray.sort(by("countryCode",-1)); //Sort by countryCode with Descending order
/*Following loop will result
np - Nepal
in - India
dz - Algeria
af - Afghanistan
*/
for(i=0,count=testArray.length;i<count;i++){
	document.write(testArray[i]['countryCode'] + " - " + testArray[i]['countryName'] + "<br>");
}
document.write("-----------------------------------------------------------------------------------<br>");

testArray.sort(by("countryName",1)); //Sort by countryName with Ascending order
/*Following loop will result
af - Afghanistan
dz - Algeria
in - India
np - Nepal
*/
for(i=0,count=testArray.length;i<count;i++){
	document.write(testArray[i]['countryCode'] + " - " + testArray[i]['countryName'] + "<br>");
}
document.write("-----------------------------------------------------------------------------------<br>");

testArray.sort(by("countryName",-1)); //Sort by countryCode with Descending order
/*Following loop will result
np - Nepal
in - India
dz - Algeria
af - Afghanistan
*/
for(i=0,count=testArray.length;i<count;i++){
	document.write(testArray[i]['countryCode'] + " - " + testArray[i]['countryName'] + "<br>");
}

 

How to create two dimensional associative array in javascript ?

var testArray = [];

testArray[0] = { 'countryCode':'af', 'countryName':'Afghanistan' };
testArray[1] = { 'countryCode':'np', 'countryName':'Nepal' };
testArray[2] = { 'countryCode':'in', 'countryName':'India' };
testArray[3] = { 'countryCode':'dz', 'countryName':'Algeria' };

alert(testArray[0]['countryCode'] + " - " + testArray[0]['countryName']); // This will result 'af'  and 'Afghanistan'

alert(testArray[2]['countryCode'] + " - " + testArray[2]['countryName']); // This will result 'in'  and 'India'

//Or you can also define it like

var testArray = [];

testArray["record1"] = { 'countryCode':'af', 'countryName':'Afghanistan' };
testArray["record2"] = { 'countryCode':'np', 'countryName':'Nepal' };
testArray["record3"] = { 'countryCode':'in', 'countryName':'India' };
testArray["record4"] = { 'countryCode':'dz', 'countryName':'Algeria' };

alert(testArray["record1"]['countryCode'] + " - " + testArray["record1"]['countryName']); // This will result 'af'  and 'Afghanistan'

alert(testArray["record3"]['countryCode'] + " - " + testArray["record3"]['countryName']); // This will result 'in'  and 'India'

 

3 ways to define a JavaScript class

JavaScript is a very flexible object-oriented language when it comes to syntax. In this article you can find three ways of defining and instantiating an object. Even if you have already picked your favorite way of doing it, it helps to know some alternatives in order to read other people’s code.

It’s important to note that there are no classes in JavaScript. Functions can be used to somewhat simulate classes, but in general JavaScript is a class-less language. Everything is an object. And when it comes to inheritance, objects inherit from objects, not classes from classes as in the “class”-ical languages.

1. Using a function

2. Using object literals

3. Singleton using a function

Click to know more

 

What is the difference between == and === in JavaScript ?

==  is normal equality check , or we can say type-converting equality check.

Here if we check

0==false // this will result true

1=="1" // this will also result true

Here 0 & false, 1 & "1" are of different type , but == operator will compare for equality after doing any necessary type conversions.

 

=== is strict equality check,  here it means that two values must have same type to check equality

Here if we check

0===false // this will result false

1==="1" // this will also result false

false===false // this will result true

1===1 // this will result true

The === operator will not do the conversion, so if two values are not the same type, === will simply return false.