JS Array Functions


Array initialization, you can use the new keyword or use brackets []:

var arr = new Array(1,2,3,4,5);
var arr = new Array();
var arr = [];
var arr = [1,2,3,4,5];


javascript array is indexed from 0 to array.length -1. To get the value at position i:

var arr = new Array(1,2,3,4,5);
var x = arr[2]; // x = 3


length property: get and set array length :

var arr = new Array(1,2,3,4,5);
var x = arr.length; // x = 5


push() method: add element to the array end, return array length:

var arr = new Array(1,2,3,4,5);
arr.push(13); //add 13 to the end of array


pop() method: delete 1 element from the array end, return the element:

var arr = new Array(1,2,3,4,5);
arr.pop(); //delete 5


shift() method: delete 1 element from the array start, return the element:

var arr = new Array(1,2,3,4,5);
arr.shift(); //delete 1, arr now is [2,3,4,5]


unshift() method: add 1 element to the array start, return the array length:

var arr = new Array(1,2,3,4,5);
arr.unshift(10); //arr now is [10,1,2,3,4,5]


indexOf() method: Search array and return the position of the found value, if not found, return -1, if more than 1 element were found, return the position of the first found value:

var arr = new Array(1,2,3,2,5);
var p = arr.indexOf(3) //p = 2
p = arr.indexOf(7) //p = -1
p = arr.indexOf(2) //p = 1


lastIndexOf() method: Search array from the end, and return the position of the first found value, if not found, return -1:

var arr = new Array(1,2,3,2,5,11,14);
var p = arr.lastIndexOf(3) //p = 2
p = arr.lastIndexOf(7) //p = -1
p = arr.lastIndexOf(2) //p = 3


reverse()method: reverse the order of the array:

var arr = new Array(1,2,3,2,5,11,14);
arr.reverse(); //arr now is [14,11,5,2,3,21]


sort() method: sorts the array:

var arr = new Array(1,2,3,2,5,11,14);
arr.sort(); //arr now is [1,2,2,3,5,11,14]


join() method: joins all elements into a string:

var arr = new Array("e","n","d","memo");
var s = arr.join(); //s is "endmemo"


concat() method: join arrays:

var arr1 = [4,2,8];
var arr2 = new Array(5,3,3);
var arr3 = [100,500];
var newarr = arr1.concat(arr2,arr3);
//newarr is [4,2,8,5,3,3,100,500]


slice(s,e) method: cut array, s - start position, e - end position (optional), if not provided, it's the end of the array:

var arr = [4,2,8,5,3,3,100,500];
var newarr = arr.slice(2,5); //newarr is [8,5,3]
var newarr = arr.slice(2); //newarr is [8,5,3,3,100,500]


splice(i,n,...) method: add/delete elements from an array, return new array. "i" - array position,"n" - number of elements to be removed, if 0, add items instead, "..." - elements to be added (optional):

var arr = [4,2,8,5,3,3,100,500];
var arr2 = arr.splice(2,4); //arr2 is [4,2,100,500]
var arr3 = arr.splice(3,0,11,12);
//arr3 is [4,2,8,11,12,5,3,3,100,500]


toString() method: join all array elements to a string, seperated by ",", return the string:

var arr = [4,2,8,5,3,3,100,500];
var s = arr.toString(); //s="4,2,8,5,3,3,100,500"


valueOf() method: returns all the array elements:

var arr = [4,2,8];
var x = arr.valueOf(); //x = "4 2 8"


Loop through an array:

var arr = [2,4,5,8,1,3,9];
for (var i=0; i<arr.length; i++)
{
if (arr[i] % 2 == 0) alert(arr[i]); //2,4,8
}
for (var i in arr)
{
if (arr[i] % 2 == 0) alert(arr[i]); //2,4,8
}


constructor property: returns the array constructing prototype:

var arr = [4,2,8];
var x = arr.constructor;
//x = "function Array() {[native code]}"


prototype property: add property or method to array:

Array.prototype.sqrt = function()
{
for (var i=0; i<this.length;i++)
{
this[i] = Math.sqrt(this[i]);
}
}
var arr = [4,16,9];
arr.sqrt(); //arr now is [2,4,3]


endmemo.com © 2024  | Terms of Use | Privacy | Home