Arrays are essential structure and field of study for any programming languages…
- array structure
var products = [ "html", "php"]; //adding a element to end of an array products[products.length] = "jquery"; products.push("jquery"); - In js associative arrays are just objects
var products = []; products["name"] = "pen"; products["price"] = 20; // accessing array elements by both array & objective way alert( products["name"] ); // accessing by "." , this is usually used to access object property alert( products.name);
- Practical example of arrays
//HTML <ul id="myList"> <li>php</li> <li>html</li> <li>css</li> <li>mysql</li> <li>java</li> </ul> //JS var myList = document.getElementById("myList"); alert(myList); // [Object HTMLULListElement] var items = myList.getElementsByTagName("li"); alert(items); // [Object HTML collection] :- a collection of HTML elements alert(items.length); // returns number of li items var item = items[0]; alert(item); // [Object HTML Li Element ] alert(item.innerHTML); // content of that li - join() : it joins an array into a string
var places = ["dhk", "ctg", "syl"]; alert(places.join(",")); // it is dhk,ctg,syl a string converted from an array - length : returns the length of an array
alert(places.length);// 3
- concat() : concatenates two arrays
var places = ["dhk", "ctg", "syl"]; var places2 = ["raj", "bar"]; var newPlace = places.concat(places2); alert(newPlace); // alerts all cities
- pop() : returns the last element of array and deletes it from original array also
var places = ["dhk", "ctg", "syl"]; alert(places.pop()); // alerts the last element and also deletes it from original array alert(places);
- push() : pushes and element in the last place of array
- shift() : Opposite of pop(). Unlike pop() it just removes and returns first element.
- unshift() : Simillar to push(). push() puts an item at the end whereas unshift() puts an element at the beginning and also shifts all other elements to the right.
- sort() & reverse():
var alpha = ["a", "z", "b"]; alert(alpha.sort()); // a,b,z alert(alpha); // a,b,z alert(alpha.reverse()); // z,b,a alert(alpha); // z,b,a
- sorting numbers
var num = [50, -5, 65, 20, 1]; alert(num.sort()); // works on updated ES alert(num.sort(function(a, b){ return a-b; // using b-a will return descending sorting } ) ); // but it is safer and ensured - slice(indexInc, indexExclusive) : This method doesnt affects original array. slice(1, 3) means return elements startign from index 1 and upto 3 (index 3 will be excluded).
var alpha = ["a", "b", "c", "d", "e"]; alert(alpha.slice(1, 3)); // b,c alert(alpha); // a,b,c,d,e
- splice(indexIncluding, Items) : returns specified number (the 2nd arg) element from starting index. It modifies original array.
var alpha = ["a", "b", "c", "d", "e"]; alert(alpha.splice(1, 3)); // b,c,d alert(alpha); // a,e
- splice(indexIncluding, Items, moreParams) : if we send more than two args to splice(), then the extra params will be put in place of removed elements.
var alpha = ["a", "b", "c", "d", "e"]; alert(alpha.splice(1, 3, "x", "y", "z")); // b,c,d alert(alpha); // a,x,y,z,e