Wednesday, 30 March 2016

Some features of arrays in java script

Some features of Arrays in java script

var entries = new Array('Trains', 'Planes', 'Automobiles', 'Carts');
console.log(entries instanceof Array);
console.log(entries);
//You can leave out the new keyword and it still works
var entries = Array("Ajmal", "Dilshad", "Russell", "Anders");
console.log(entries instanceof Array);
console.log(entries);
//We can make array with [] also.
var places = ['Malmö', 'Lund', 'Stockholm', 'Gothenborg'];
console.log(places instanceof Array);
console.log(places);
console.log(places[1]);
//No exception gets thrown if you access a non-existing index
console.log(places[9]);//Get undefined.
//Initializing array of a particular length
var books = new Array(5);
console.log(books.length);//returns 5
var books = new Array("5");//one string element. Not length.
console.log(books.length);
var entries = [,,,];
console.log(entries.length);

//We can assign a numeric length to the length property of an array
var transport = ['Planes', 'Trains', 'Carts', 'Buses'];
console.log(transport.length);
transport.length = 10;
console.log(transport.length);
transport.length = 2;
//Accessing a non-existent index of the array gives undefined.
console.log(transport[3]);
//Assigning a new means of transport to a non-existing index.
transport[49] = 'Cars';//expands the array.
console.log(transport[49]);
console.log(transport.length);
//Appending to the end of the array
transport[transport.length] = 'Bicycles';
console.log(transport.length);

//To get a comma separated list of the elements of an array
console.log(transport.toString());

var ratings = [5, 2, 4];
console.log(ratings);
console.log(ratings.valueOf());

//For separating the elements of the array with another string
console.log(ratings.join('|'));
console.log(ratings.join('   '));

//Array acting as a stack.
var ratings = [];
ratings.push(5);
ratings.push(2, 4);
console.log(ratings.length);
ratings.shift(1);
console.log(ratings.length);
//To check that the element at index 0 does not exit now.
console.log(ratings.valueOf());
console.log(ratings.pop());//Gets us the last inserted element
console.log(ratings.length);
//Inserting a new element at the beginning
ratings.unshift(9);
console.log(ratings.valueOf());
ratings.unshift(8);
console.log(ratings[0]);
console.log(ratings.valueOf());
var newCount = ratings.unshift(7);
console.log(ratings.valueOf());
console.log(newCount);

//Concatenation on arrays
var ratings = [1, 2, 3];
var newRatings = ratings.concat([4, 5, 6]);
console.log(newRatings.length);
console.log(newRatings.valueOf());
newRatings = newRatings.concat([7, 8], 9, 10);
console.log(newRatings.valueOf());

//Slicing an array to get the remaining element
var ratings = [1, 2, 3, 4, 5, 6, 7];
var newRatings = ratings.slice(2);//2 means index 2 
console.log(newRatings.valueOf());
console.log(newRatings.toString());
var newRatings = ratings.slice(1, 4);//Gives 2,3. Excludes index 3
console.log(newRatings.toString());
console.log(newRatings.valueOf());
//Passing a negative argument to slice function give elements from the end of the array
var newRatings = ratings.slice(-2);
console.log(newRatings.valueOf());

//Starting at ith index and deleting x number of elements
var ratings = [1, 2, 3, 4];
ratings.splice(1, 2);//Start from index 1 and delete 2 items.
console.log(ratings.toString());//The riginal array gets modified.
//Getting the removed elements as an array with splice function
var ratings = [1, 2, 3, 4];
var removedRatings = ratings.splice(0, 2);
console.log(removedRatings.toString());
console.log(ratings.toString());

//Function splice with three arguments.
var ratings = [1, 2, 3, 4];
//start at index 2, delete nothing and insert 99 at index 2
ratings.splice(2, 0, 99);
console.log(ratings.toString());
//We can insert more than one items also
var ratings = [1, 2, 3, 4];
ratings.splice(2, 0, 99, 100, 101);
console.log(ratings.toString());
//We can delete items and insert items at the same time
var ratings = [1, 2, 3, 4];
//Start at index 2, delete one item and insert 3 items.
ratings.splice(2, 1, 99, 200, 300);
console.log(ratings.toString());

//Reversing elements of an array
var ratings = [1, 2, 3, 4,];
ratings.reverse();
console.log(ratings);

//Sorting the elments of an array that is not the correct way
//The sort function is treating elements liks Strings while sorting
var ratings = [1, 4, 10, 14,];
ratings.sort();
console.log(ratings.toString());
//The correct way to use function is sort is to pass it a function
var ratings = [1, 4, 10, 14,];
ratings.sort(
       function(value1, value2){
               return value1 - value2;
              }
            );
console.log(ratings.toString());

//Searching an item in an  array
var ratings = [1, 2, 3, 4, 5, 4, 3, 2, 1];
console.log(ratings.indexOf(3));//returns 2
console.log(ratings.indexOf('3')); //returns -1 as it can not find '3' in the array
console.log(ratings.indexOf(66));//returns -1 as there is no 66 in the array.
var names = new Array("Ajmal", "Dilshad", "Russell", "Anders", "Russell");
console.log(names.indexOf("Dilshad"));
//lastIndexOf() function starts at the end of the array to search
console.log(names.lastIndexOf("Russell"));

No comments:

Post a Comment