This is a FREE helpline run by volunteers and supported by the community. The folks on the other end of the phone are fellow JavaScript developers donating a little time for the good of our community and the web as a whole.
Author Archives: super-admin
How to sort two dimensional associative array in javascript ?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
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 ?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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' |
How to create two dimensional array in JavaScript?
|
1 2 3 4 5 6 7 8 9 10 |
//Define 2 dimensional array var testArray = [[1,"A"],[2,"B"],[3,"C"]]; alert(testArray[0][0]); // it will result 1 alert(testArray[0][1]); // it will result A //Similarly to get 3 and C use following statement alert(testArray[2][0]); // it will result 3 alert(testArray[2][1]); // it will result C |
Object-orientation and inheritance in JavaScript: a comprehensive explanation
Object-orientation and inheritance are important part of JavaScript & this article has given good explanation of both by giving very good examples. So just Click Here to read the entire article.
Yahoo!’s Mojito – Open Source JavaScript Framework
Yahoo!’s Mojito is available as open source Javscript Frame Work, a ground-breaking JavaScript framework developed by Yahoo! for Web developers. Mojito is one of the Yahoo! Cocktails, our JavaScript-centric presentation platform for connected devices.
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
What is the difference between == and === in JavaScript ?
== is normal equality check , or we can say type-converting equality check.
Here if we check
|
1 2 3 |
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
|
1 2 3 4 5 6 7 |
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.
How to pass variable (Multiple) number of arguments to function ?
Look at the following function, which accept any number of parameters.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
function testArgument(){ //Run a for loop to check different numbers of argument for (var i = 0; i < arguments.length; i++) { alert(arguments[i]); } } //Function call with single argument testArgument("a"); //Function call with multiple argument testArgument("a","b","c","d"); |
JSLint – The JavaScript Code Quality Tool
JSLint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules. It was developed by Douglas Crockford. It is provided primarily as an online tool, but there are also command-line adaptations.
Click to know more about JSLint