WHAT'S NEW?
Loading...

Array Methods - lastindexOf (), map (), pop (), push (), reduce (), reduceRight () in javascript with example - Tech-n-Savvy Blogger


lastIndexOf ()

Javascript array lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

Syntax

Its syntax is as follows:

array.join(separator);


Parameter Details

·         searchElement : Element to locate in the array.

·     fromIndex : The index at which to start searching backwards. Defaults to the array's length, i.e., the whole array will be searched. If the index is greater than or equal to the length of the array, the whole array will be searched. If negative, it is taken as the offset from the end of the array.

Return Value

Returns the index of the found element from the last.

Compatibility

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work, you need to add the following code at the top of your script.


if (!Array.prototype.lastIndexOf)

{

Array.prototype.lastIndexOf = function(elt /*, from*/)

{

var len = this.length;

var from = Number(arguments[1]);

if (isNaN(from))

{

from = len - 1;

}

else

{

from = (from < 0)

?  Math.ceil(from)

: Math.floor(from);

if (from < 0)

from += len;

else if (from >= len)

from = len - 1;

}


for (; from > -1; from--)

{

if (from in this &&

this[from] === elt)

return from;

}

return -1;

};

}



Example

Try the following example.

<html>

<head>

<title>JavaScript Array lastIndexOf Method</title>

</head>

<body>

<script type="text/javascript">

if (!Array.prototype.lastIndexOf)

{

Array.prototype.lastIndexOf = function(elt /*, from*/)

{

var len = this.length;


var from = Number(arguments[1]);

if (isNaN(from))

{

from = len - 1;

}

else

{

from = (from < 0)

?  Math.ceil(from)

: Math.floor(from);

if (from < 0)

from += len;

else if (from >= len)

from = len - 1;

}


for (; from > -1; from--)

{

if (from in this &&

this[from] === elt)

return from;

}

return -1;

};

}


var index = [12, 5, 8, 130, 44].lastIndexOf(8); document.write("index is : " + index );


var index = [12, 5, 8, 130, 44, 5].lastIndexOf(5);

document.write("<br />index is : " + index );

</script>

</body>

</html>

Output

index is : 2

index is : 5


map ()

Javascript array map() method creates a new array with the results of calling a provided function on every element in this array.

Syntax

Its syntax is as follows:

array.map(callback[, thisObject]);

Parameter Details

·         callback : Function that produces an element of the new Array from an element of the current one.

·         thisObject : Object to use as this when executing callback.



Return Value

Returns the created array.

Compatibility

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work, you need to add the following code at the top of your script.

if (!Array.prototype.map)

{

Array.prototype.map = function(fun /*, thisp*/)

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


var res = new Array(len);

var thisp = arguments[1];

for (var i = 0; i < len; i++)

{

if (i in this)

res[i] = fun.call(thisp, this[i], i, this);

}


return res;

};

}

Example

Try the following example.

<html>

<head>

<title>JavaScript Array map Method</title>

</head>

<body>

<script type="text/javascript">

if (!Array.prototype.map)

{

Array.prototype.map = function(fun /*, thisp*/)

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


var res = new Array(len);

var thisp = arguments[1];

for (var i = 0; i < len; i++)

{

if (i in this)

res[i] = fun.call(thisp, this[i], i, this);

}


return res;

};

}


var numbers = [1, 4, 9];

var roots = numbers.map(Math.sqrt);


document.write("roots is : " + roots );


</script>

</body>

</html>

Output

roots is : 1,2,3


pop ()

Javascript array pop() method removes the last element from an array and returns that element.


Syntax

Its syntax is as follows:

Array.pop();

Return Value

Returns the removed element from the array.

Example

Try the following example.

<html>

<head>

<title>JavaScript Array pop Method</title>

</head>

<body>

<script type="text/javascript">

var numbers = [1, 4, 9];


var element = numbers.pop(); document.write("element is : " + element );


var element = numbers.pop();

document.write("<br />element is : " + element );

</script>

</body>

</html>

Output

element is : 9

element is : 4


push ()

Javascript array push() method appends the given element(s) in the last of the array and returns the length of the new array.

Syntax


Its syntax is as follows:

Array.push();

Parameter Details

element1, ..., elementN: The elements to add to the end of the array.

Return Value

Returns the length of the new array.

Example

Try the following example.

<html>

<head>

<title>JavaScript Array push Method</title>

</head>

<body>

<script type="text/javascript">

var numbers = new Array(1, 4, 9);


var length = numbers.push(10); document.write("new numbers is : " + numbers );


length = numbers.push(20);

document.write("<br />new numbers is : " + numbers );

</script>

</body>

</html>





Output

new numbers is : 1,4,9,10

new numbers is : 1,4,9,10,20


reduce ()

Javascript array reduce() method applies a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.

Syntax

Its syntax is as follows:

array.reduce(callback[, initialValue]);

Parameter Details

·         callback : Function to execute on each value in the array.

·         initialValue : Object to use as the first argument to the first call of the callback.

Return Value

Returns the reduced single value of the array.

Compatibility

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work, you need to add the following code at the top of your script.


if (!Array.prototype.reduce)

{

Array.prototype.reduce = function(fun /*, initial*/)

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


//  no value to return if no initial value and an empty array if (len == 0 && arguments.length == 1)

throw new TypeError();


var i = 0;

if (arguments.length >= 2)

{

var rv = arguments[1];

}

else

{

do

{

if (i in this)

{

rv = this[i++];

break;

}


//  if array contains no values, no initial value to return if (++i >= len)

throw new TypeError();

}

while (true);

}


for (; i < len; i++)

{

if (i in this)

rv = fun.call(null, rv, this[i], i, this);

}


return rv;

};

}

Example

Try the following example.

<html>

<head>

<title>JavaScript Array reduce Method</title>

</head>

<body>

<script type="text/javascript">

if (!Array.prototype.reduce)

{

Array.prototype.reduce = function(fun /*, initial*/)

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


//  no value to return if no initial value and an empty array if (len == 0 && arguments.length == 1)

throw new TypeError();


var i = 0;

if (arguments.length >= 2)

{

var rv = arguments[1];

}

else

{

do

{

if (i in this)

{

rv = this[i++];

break;

}


//  if array contains no values, no initial value to return if (++i >= len)

throw new TypeError();

}

while (true);

}


for (; i < len; i++)

{

if (i in this)

rv = fun.call(null, rv, this[i], i, this);

}


return rv;

};

}


var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; }); document.write("total is : " + total ); </script>

</body>

</html>

Output

total is : 6


reduceRight ()

Javascript array reduceRight() method applies a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

Syntax

Its syntax is as follows:

array.reduceRight(callback[, initialValue]);

Parameter Details

·         callback : Function to execute on each value in the array.

·         initialValue : Object to use as the first argument to the first call of the callback.

Return Value

Returns the reduced right single value of the array.

Compatibility

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work, you need to add the following code at the top of your script.


if (!Array.prototype.reduceRight)

{

Array.prototype.reduceRight = function(fun /*, initial*/)

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


//  no value to return if no initial value, empty array if (len == 0 && arguments.length == 1)

throw new TypeError();


var i = len - 1;

if (arguments.length >= 2)

{

var rv = arguments[1];

}

else

{

do

{

if (i in this)

{

rv = this[i--];

break;

}


//  if array contains no values, no initial value to return if (--i < 0)

throw new TypeError();

}

while (true);

}


for (; i >= 0; i--)

{

if (i in this)

rv = fun.call(null, rv, this[i], i, this);

}


return rv;

};

}

Example

Try the following example.

<html>

<head>

<title>JavaScript Array reduceRight Method</title>

</head>

<body>

<script type="text/javascript">

if (!Array.prototype.reduceRight)

{

Array.prototype.reduceRight = function(fun /*, initial*/)

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


//  no value to return if no initial value, empty array if (len == 0 && arguments.length == 1)

throw new TypeError();


var i = len - 1;

if (arguments.length >= 2)

{

var rv = arguments[1];

}

else

{

do

{

if (i in this)

{

rv = this[i--];

break;

}


//  if array contains no values, no initial value to return if (--i < 0)

throw new TypeError();

}

while (true);

}


for (; i >= 0; i--)

{

if (i in this)


rv = fun.call(null, rv, this[i], i, this);

}


return rv;

};

}


var total = [0, 1, 2, 3].reduceRight(function(a, b){ return a + b; }); document.write("total is : " + total ); </script>

</body>

</html>

Output

total is : 6


0 comments:

Post a Comment