How can I add Key/Value pair to Object

var obj = {
    key1: value1,
    key2: value2
};

There are two ways to add new properties to an object

Method 1 – Using dot notation

 obj.key3 = "value3";

Method 2 – Using square bracket notation

 obj["key3"] = "value3";

The first form is used when you know the name of the property. The second form is used when the name of the property is dynamically determined. Like in this example:

var getProperty = function (propertyName) {
    return obj[propertyName];
};

getProperty("key1");
getProperty("key2");
getProperty("key3");

A real JavaScript array can be constructed using either:

The Array literal notation:

var arr = [];

The Array constructor notation:

var arr = new Array();

Simple 2 Dimensional Array

 var grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];
 print(grades[2][2]); 

Method 1

var numeric = [
    ['input1','input2'],
    ['input3','input4']
];
numeric[0][0] == 'input1';
numeric[0][1] == 'input2';
numeric[1][0] == 'input3';
numeric[1][1] == 'input4';

Method 2

var obj = {
    'row1' : {
        'key1' : 'input1',
        'key2' : 'inpu2'
    },
    'row2' : {
        'key3' : 'input3',
        'key4' : 'input4'
    }
};
obj.row1.key1 == 'input1';
obj.row1.key2 == 'input2';
obj.row2.key1 == 'input3';
obj.row2.key2 == 'input4';

Method 3

var mixed = {
    'row1' : ['input1', 'inpu2'],
    'row2' : ['input3', 'input4']
};
obj.row1[0] == 'input1';
obj.row1[1] == 'input2';
obj.row2[0] == 'input3';
obj.row2[1] == 'input4';

Creating a Multidimensional Array on Fly

var row= 20;
var column= 10;
var f = new Array();

for (i=0;i<row;i++) {
 f[i]=new Array();
 for (j=0;j<column;j++) {
  f[i][j]=0;
 }
}

Method to create a Javascript Multidimensional Array

Array.matrix = function(numrows, numcols, initial) {
var arr = [];
for (var i = 0; i < numrows; ++i) {
var columns = [];
for (var j = 0; j < numcols; ++j) {
columns[j] = initial;
}
arr[i] = columns;
}
return arr;
}

Sample Test Values

var nums = Array.matrix(5,5,0);
print(nums[1][1]); // displays 0
var names = Array.matrix(3,3,"");
names[1][2] = "Joe";
print(names[1][2]); // display "Joe"

1.Form reset javascript does not work for hidden field

 function resetForm()
 {
   document.getElementById("SetUpForm").reset();
 }
 

The reset() functionality in javascript is intended for clearing user input, and since hidden inputs are not directly accessible by the user, it doesn’t make sense to allow the user to reset the hidden input’s value.

2.Dropdown should be reset by Javascript if selected is used.
The Same way this does not work in dropdown where the value is set to selected.The values of the dropdown should be manually reset by adding below code to above

 function resetForm()
 {
   document.getElementById("SetUpForm").reset();

   $("#cboAge").val('');
   $("#cboCity").val('');
 }
 
  1. In the below page the Checkbox checked values should be maintained as comma separated values.
  2. If select all checkbox at the top is clicked all the checkbox in that column should be marked as clicked.
  3. In the same way if all individual checkbox are selected the select All checkbox should be Marked as Clicked.

function changeInvestmentIds(pThis) 
{   	
    	if($(pThis).is(":checked"))
    	{	
      	      //Hidden variable which stores the CSV values of Checkbox
  	      if($('#csvSelectedInvestmentIds').val() != '')    	  
  	    	 arrInvstIds = $('#csvSelectedInvestmentIds').val().split(',');	
    		
	       arrInvstIds.push($(pThis).val());
	      
	      $('#csvSelectedInvestmentIds').val(arrInvstIds.join(","));
    	}
        else
        //Incase the checkbox is unchecked the values shld be removed from hidden variable 
    	{	
    		arrInvstIds = $('#csvSelectedInvestmentIds').val().split(',');
    		
    		for(var i = arrInvstIds.length; i--;) {
  	          if(arrInvstIds[i] === $(pThis).val()) {
  	        	  arrInvstIds.splice(i, 1);
  	          }
  	      }	
    		
    	  $('#csvSelectedInvestmentIds').val(arrInvstIds.join(","));
      	}    	
    	
        //This is called here incase all checkbox are checked the 
        //select all should also be checked
    	chkIsAllChecked();


    	setParentWindowVal();
}

//Incase selectAll checkbox is ticked all the checkbox should be marked as checked.
function toggleSelectAll(source) 
{		
	  csvInvestmentIds = '';	
	  var arrInvIds = [];
		  
	  if($(source).prop('checked'))	  
	   $('.chkInvestmentClass').prop('checked', true);	  
	  else	  
	   $('.chkInvestmentClass').prop('checked', false);		   
	  
            
	  if($(source).prop('checked'))
	  {
                  //The values of all the checkbox are  added to array and
                  //moved to csv hidden variable 
		  $(".chkInvestmentClass").each(function() {		  
			  arrInvIds.push($(this).val());
		  });
		  
		  $('#csvSelectedInvestmentIds').val(arrInvIds.join(","));
	  }else		  
	    $('#csvSelectedInvestmentIds').val('');
	  
	  setParentWindowVal();	  
}


//This function is to check if all check boxes when checked
//individually the select all check box should be checked
function chkIsAllChecked()
{
 if($('.chkInvestmentClass:checked').length == $('.chkInvestmentClass').length)		  
  $('#chkSelectAll')[0].checked=true;
 else
  $('#chkSelectAll')[0].checked=false;
}

HTML Code

  1. changeInvestmentIds(pThis) function is called in each checkbox when click event is carried out.
  2. toggleSelectAll(source) is called when selectAll checkbox is Clicked
  3. chkIsAllChecked() is called internally in changeInvestmentIds(pThis)
<table>
<thead>
<tr>
<th>
Locations
</th>
<th>
<input type="checkbox" name="chkSelectAll" value="SelectAll" styleId="selectAllChkBx" onclick="toggleSelectAll(this);"/>
</th>
<tr>
</thead>
<tbody>
 <tr>
 <td>Chennai</td>
 <td> 
  <input type="checkbox" name="chkInvestmentIds" value='Chennai' />' class="chkInvestmentClass" 
				onclick="changeInvestmentIds(this)">
</td>
</tr>
 <tr>
 <td>Bangalore</td>
 <td> 
  <input type="checkbox" name="chkInvestmentIds" value='Bangalore' />' class="chkInvestmentClass" 
				onclick="changeInvestmentIds(this)">
</td>
</tr>
 <tr>
 <td>Mumbai</td>
 <td> 
  <input type="checkbox" name="chkInvestmentIds" value='Mumbai' />' class="chkInvestmentClass" 
				onclick="changeInvestmentIds(this)">
</td>
</tr>
</tbody>
</table>
//Function declaration
function foo() { return 5; }

//Anonymous function expression
var foo = function() { return 5; }

//Named function expression
var foo = function foo() { return 5; }

function declarations loads before any code is executed.While function expressions loads only
when the interpreter reaches that line of code.

So if you try to call a function expression before it’s loaded, you’ll get an error

But if you call a function declaration, it’ll always work. Because no code can be called until all declarations are loaded.

Function Expression

  alert(foo()); // ERROR! foo wasn't loaded yet
  var foo = function() { return 5; } 

Function Declaration

  alert(foo()); // Alerts 5. Declarations are loaded before any code can run.
  function foo() { return 5; } 

The difference is in First case foo() is defined at run-time, whereas in second case foo() is defined at parse-time
for a script block

Getting GMT Time for the Day

 var strDate = new Date();

 hour = strDate.getUTCHours().toString();
 minute = strDate.getUTCMinutes().toString();
 second = strDate.getUTCSeconds().toString();

 if (hour.length == 1) { hour = "0" + hour; }
 if (minute.length == 1) { minute = "0" + minute; }
 if (second.length == 1) { second = "0" + second; }

 Cal.SetHour(hour); 
 Cal.SetMinute(minute); 
 Cal.SetSecond(second);

Common elements in two arrays javascript

arrNum1 = new Array(1,2,3);
arrNum2 = new Array(2,3,4,5);
arrNum3 = new Array();

arrNum3 =  intersect_safe(arrNum1, arrNum2);

for(i=0;i<arrNum3.length;i++)
 alert(arrNum3[i]);

function intersect_safe(a, b)
{
  var ai=0, bi=0;
  var result = new Array();

   while(ai<a.length && bi < b.length )
  {
     if(a[ai] != b[bi])
     { 
       bi++; 
     }
     else
     {
       result.push(a[ai]);
       ai++;
       bi++;
     }
  }

  return result;
}
var arr = new Array();
arr[0] = 100;
arr[1] = 0;
arr[2] = 50;

Array.prototype.max = function() {
  return Math.max.apply(null, this)
}

Array.prototype.min = function() {
  return Math.min.apply(null, this)
}

var min = Math.min.apply(null, arr),
    max = Math.max.apply(null, arr);

To find the Maximum value in Array Use Below

 var max_of_array = Math.max.apply(Math, array);