Categories
ECMAScript Javascript

Array Functions

To demonstrate what are the available methods to operate over arrays.

/**
 * To demonstrate working of methods on Arrays
 */

var color = ["Red","Green","Yellow","Purple","Violet","Red"];
var shape = ["Triangle","Square","Rectangle","Pyramid"];
var evenNums = [2,4,6,8,10];
var oddNums = [1,3,5,7,9];
var evenOddMix = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];

//lambda functions to declare even/odd syntaxes
var isEven =(X)=>X%2==0;
var isOdd =(X)=>!isEven(X);
var square = (X)=>X*X;
var print = (X)=>console.log("Square of :: "+X+" is ::"+square(X));
var sum = (X,Y)=>X+Y;
var joinbyAnd=(X,Y)=> X+" & "+ Y;

//Concat functions array.concat(value1, value2, ..., valueN);
var colorShapesDigits = color.concat(shape,["1","2"]);
console.log("colorShapesDigits ::"+colorShapesDigits);

//every functions array.every(callback[, thisObject]);
console.log("is Every Even :: From evenNums :: " +evenNums.every(isEven));
console.log("is Every Even :: From evenOddMix :: " +evenOddMix.every(isEven));
console.log("is Every Even :: From oddNums :: " +oddNums.every(isEven));

//some functions array.some(callback[, thisObject]); 
console.log("are some Even :: From evenOddMix :: " +evenOddMix.some(isEven));
console.log("are some Even :: From oddNums :: " +oddNums.some(isEven));


//filter functions array.filter(callback[, thisObject]);
console.log("filter odds :: From evenNums :: " +evenNums.filter(isOdd));
console.log("filter Evens :: From evenOddMix :: " +evenOddMix.filter(isEven));

//forEach functions array.forEach(callback[, thisObject]); 
console.log("forEach printing :: from evenOddMix ::");
evenOddMix.forEach(print);

//indexOf function array.indexOf(searchElement[, fromIndex]);
console.log("index of :: Purple :: from color ::"+color.indexOf("Purple"));
console.log("index of :: Black :: from color ::"+color.indexOf("Black"));
console.log("index of :: Green :: from color ::"+color.indexOf("Green",2));

//lastIndexOf function array.lastIndexOf(searchElement[, fromIndex]);  
console.log("last index of :: Red :: from color ::"+color.lastIndexOf("Red"));

// join functions to join all elements of array into single string array.join([seperator])
console.log("join :: from oddNums ::"+oddNums.join());
console.log("join by - :: from oddNums ::"+oddNums.join("-"));

//map function to map array into another array array.map(callback[, thisObject]); 
console.log("Square All :: From evenOddMix :: " +evenOddMix.map(square));


//pop function to remove last element and return it, array.pop()
console.log("pop :: from shape ::"+shape.pop());
console.log("shape array after pop now ::"+shape);
 
//shift function to remove first element from array, array.shift()
console.log("shift :: from shape :: "+shape.shift());
console.log("shape array after shift now ::"+shape);

console.log("shift :: from shape :: "+shape.unshift("Circle"));
console.log("shape array after unshift now ::"+shape);


//push function to append elements in array, array.push(element1, ..., elementN);
shape.push("Rhombus","Trapezium");
console.log("shape array after push now :"+shape);

//reduce function to apply entire array to single value, array.reduce(callback[, initialValue]); 
console.log("reduce to sum :: from evenNums ::"+evenNums.reduce(sum));
console.log("reduce from right to single string :: from color :: all color are ::"+color.reduceRight(joinbyAnd));
console.log("reduce from left to single string :: from color :: all color are ::"+color.reduce(joinbyAnd));

//to return the reversed array array.reverse();
console.log("Revere :: from array shape :: "+shape.reverse());

//slice to return the section of an array, array.slice(begin[,end]), end is optional & exclusive
console.log("slice 0 to 2 :: from color array ::"+color.slice(0,2));

//sort function to return sorted array array.sort([compareFunction]) if comparefunction not passed, sorted lexographically;
console.log("sort simple by lexically :: from evenOddMix :: "+evenOddMix.sort());
console.log("sort  numbers :: from evenOddMix :: "+evenOddMix.sort((X,Y)=>X-Y));

//toString function
console.log("toString :: from evenOddMix :: "+evenOddMix.toString());


Output

colorShapesDigits ::Red,Green,Yellow,Purple,Violet,Red,Triangle,Square,Rectangle,Pyramid,1,2
is Every Even :: From evenNums :: true
is Every Even :: From evenOddMix :: false
is Every Even :: From oddNums :: false
are some Even :: From evenOddMix :: true
are some Even :: From oddNums :: false
filter odds :: From evenNums :: 
filter Evens :: From evenOddMix :: 2,4,6,8,10,12,14
forEach printing :: from evenOddMix ::
Square of :: 1 is ::1
Square of :: 2 is ::4
Square of :: 3 is ::9
Square of :: 4 is ::16
Square of :: 5 is ::25
Square of :: 6 is ::36
Square of :: 7 is ::49
Square of :: 8 is ::64
Square of :: 9 is ::81
Square of :: 10 is ::100
Square of :: 11 is ::121
Square of :: 12 is ::144
Square of :: 13 is ::169
Square of :: 14 is ::196
index of :: Purple :: from color ::3
index of :: Black :: from color ::-1
index of :: Green :: from color ::-1
last index of :: Red :: from color ::5
join :: from oddNums ::1,3,5,7,9
join by - :: from oddNums ::1-3-5-7-9
Square All :: From evenOddMix :: 1,4,9,16,25,36,49,64,81,100,121,144,169,196
pop :: from shape ::Pyramid
shape array after pop now ::Triangle,Square,Rectangle
shift :: from shape :: Triangle
shape array after shift now ::Square,Rectangle
shift :: from shape :: 3
shape array after unshift now ::Circle,Square,Rectangle
shape array after push now :Circle,Square,Rectangle,Rhombus,Trapezium
reduce to sum :: from evenNums ::30
reduce from right to single string :: from color :: all color are ::Red & Violet & Purple & Yellow & Green & Red
reduce from left to single string :: from color :: all color are ::Red & Green & Yellow & Purple & Violet & Red
Revere :: from array shape :: Trapezium,Rhombus,Rectangle,Square,Circle
slice 0 to 2 :: from color array ::Red,Green
sort simple by lexically :: from evenOddMix :: 1,10,11,12,13,14,2,3,4,5,6,7,8,9
sort  numbers :: from evenOddMix :: 1,2,3,4,5,6,7,8,9,10,11,12,13,14
toString :: from evenOddMix :: 1,2,3,4,5,6,7,8,9,10,11,12,13,14

Leave a comment

Design a site like this with WordPress.com
Get started