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"

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);

The below function takes array element which needs to be removed as parameter as below

ary.remove(‘One’);
var ary = [‘One’, ‘Two’, ‘Three’];
Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};

The above method works perfectly works for java script array with strings in it.

For the below variable

var csvEmpIds = ‘1,2,3,4,5’;
arrEmpIds = csvEmpIds.split(‘,’);
ary.remove(2);

The above will not work as you should pass 2 as a string and split function creates
a array by splitting from one variable and putting as a strings in array.

so csvEmpIds.split(‘,’) will create array like below

arrEmpIds = ['1','2','3','4','5'];

In this case to remove element from array which you got by splitting come delimited value you should
do as below.

ary.remove('2');

To Remove array which is declared globally use the below function

var ary = ['three', 'seven', 'eleven'];
removeA(ary, 'seven');
function removeA(arr)
{
  var what, a = arguments, L = a.length, ax;
  
  while (L > 1 && arr.length) 
  {
    what = a[--L];
    while ((ax= arr.indexOf(what)) !== -1) 
    {
      arr.splice(ax, 1);
    }
   }

   return arr;
}

How to Split CSV into array

   csvProjectIds = '1,2,3,4';
   arrProjectIds = csvProjectIds.split(',');				

Output

     arrProjectIds[0] = 1
     arrProjectIds[1] = 2
     arrProjectIds[2] = 3
     arrProjectIds[3] = 4

How to Find Element is in Array

   csvProjectIds = '1,2,3,4';
   arrProjectIds = csvProjectIds.split(',');				
   alert(arrProjectIds.indexOf('1'));

Output

 0

The indexOf Returns 0 incase the element is in the array or
-1 if not in the array