WHAT'S NEW?
Loading...

Array methods - concat (), every (), filter (), forEach (), indexOf (), join () in javascript with example - Tech-n-Savvy Blogger


Array Methods

Here is a list of the methods of the Array object along with their description.


Method
Description







concat()
Returns  a  new  array  comprised  of  this  array


joined with other array(s) and/or value(s).




every()
Returns  true  if  every  element  in  this  array


satisfies the provided testing function.




filter()
Creates a new array with all of the elements of


this array for which the provided filtering function


returns true.




forEach()
Calls a function for each element in the array.




indexOf()
Returns  the  first  (least)  index  of  an  element


within the array equal to the specified value, or -


1 if none is found.




join()
Joins all elements of an array into a string.




lastIndexOf()
Returns the last (greatest) index of an element


within the array equal to the specified value, or -


1 if none is found.




map()
Creates a new array with the results of calling a


provided function on every element in this array.




pop()
Removes  the  last  element  from  an  array  and


returns that element.








push()
Adds  one  or  more  elements  to  the  end  of  an


array and returns the new length of the array.




reduce()
Apply  a  function  simultaneously  against  two


values  of  the  array  (from  left-to-right)  as  to


reduce it to a single value.




reduceRight()
Apply  a  function  simultaneously  against  two


values  of  the  array  (from  right-to-left)  as  to


reduce it to a single value.




reverse()
Reverses the order of the elements of an array --


the first becomes the last, and the last becomes


the first.




shift()
Removes  the  first  element  from  an  array  and


returns that element.




slice()
Extracts a section of an array and returns a new


array.




some()
Returns true if at least one element in this array


satisfies the provided testing function.




toSource()
Represents the source code of an object




sort()
Sorts the elements of an array.




splice()
Adds and/or removes elements from an array.




toString()
Returns a string representing the array and its


elements.




unshift()
Adds one or more elements to the front of an


array and returns the new length of the array.






In the following sections, we will have a few examples to demonstrate the usage of Array methods.


concat ()

Javascript array concat() method returns a new array comprised of this array joined with two or more arrays.

Syntax

The syntax of concat() method is as follows:

array.concat(value1, value2, ..., valueN);

Parameter Details

valueN : Arrays and/or values to concatenate to the resulting array.

Return Value

Returns the length of the array.

Example

Try the following example.

<html>

<head>

<title>JavaScript Array concat Method</title>

</head>

<body>

<script type="text/javascript">

var alpha = ["a", "b", "c"];

var numeric = [1, 2, 3];


var alphaNumeric = alpha.concat(numeric); document.write("alphaNumeric : " + alphaNumeric );

</script>

</body>

</html>

Output

alphaNumeric : a,b,c,1,2,3



 every ()

Javascript array every method tests whether all the elements in an array passes the test implemented by the provided function.

Syntax

Its syntax is as follows:

array.every(callback[, thisObject]);

Parameter Details

·         callback : Function to test for each element.

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

Return Value

Returns true if every element in this array satisfies the provided testing function.

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.every)

{

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

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


var thisp = arguments[1];

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

{

if (i in this &&

!fun.call(thisp, this[i], i, this))

return false;

}


return true;

};

}

Example

Try the following example.

<html>

<head>

<title>JavaScript Array every Method</title>

</head>

<body>

<script type="text/javascript">

if (!Array.prototype.every)

{

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

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


var thisp = arguments[1];

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

{

if (i in this &&

!fun.call(thisp, this[i], i, this))

return false;

}


return true;

};

}

function isBigEnough(element, index, array) { return (element >= 10);


}


var passed = [12, 5, 8, 130, 44].every(isBigEnough); document.write("First Test Value : " + passed );


passed = [12, 54, 18, 130, 44].every(isBigEnough);

document.write("Second Test Value : " + passed );

</script>

</body>

</html>

Output

First Test Value : falseSecond Test Value : true


filter ()

Javascript array filter() method creates a new array with all elements that pass the test implemented by the provided function.

Syntax

Its syntax is as follows:

array.filter (callback[, thisObject]);

Parameter Details

·         callback : Function to test for each element of an array.

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

Return Value

Returns 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.filter)


{

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

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


var res = new Array();

var thisp = arguments[1];

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

{

if (i in this)

{

var val = this[i]; // in case fun mutates this if (fun.call(thisp, val, i, this))

res.push(val);

}

}


return res;

};

}

Example

Try the following example.

<html>

<head>

<title>JavaScript Array filter Method</title>

</head>

<body>

<script type="text/javascript">

if (!Array.prototype.filter)

{


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

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


var res = new Array();

var thisp = arguments[1];

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

{

if (i in this)

{

var val = this[i]; // in case fun mutates this if (fun.call(thisp, val, i, this))

res.push(val);

}

}


return res;

};

}


function isBigEnough(element, index, array) { return (element >= 10);

}


var filtered = [12, 5, 8, 130, 44].filter(isBigEnough); document.write("Filtered Value : " + filtered );


</script>

</body>

</html>

Output


Filtered Value : 12,130,44


forEach ()

Javascript array forEach() method calls a function for each element in the array.

Syntax

Its syntax is as follows:

array.forEach(callback[, thisObject]);

Parameter Details

·         callback : Function to test for each element of an array.

·         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 following code at the top of your script.

if (!Array.prototype.forEach)

{

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

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


var thisp = arguments[1];

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

{

if (i in this)

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

}
  
};

}

Example

Try the following example.

<html>

<head>

<title>JavaScript Array forEach Method</title>

</head>

<body>

<script type="text/javascript">

if (!Array.prototype.forEach)

{

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

{

var len = this.length;

if (typeof fun != "function")

throw new TypeError();


var thisp = arguments[1];

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

{

if (i in this)

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

}

};

}


function printBr(element, index, array) { document.write("<br />[" + index + "] is " + element );

}


[12, 5, 8, 130, 44].forEach(printBr); 

</script>

</body>

</html>

Output

[0]  is 12

[1]  is 5

[2]  is 8

[3]  is 130

[4]  is 44


indexOf ()

Javascript array indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Syntax

Its syntax is as follows:

array.indexOf(searchElement[, fromIndex]);

Parameter Details

·         searchElement : Element to locate in the array.

·         fromIndex : The index at which to begin the search. Defaults to 0, i.e. the whole array will be searched. If the index is greater than or equal to the length of the array, -1 is returned.

Return Value

Returns the index of the found element.

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.indexOf)

{

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

{

var len = this.length;


var from = Number(arguments[1]) || 0;

from = (from < 0)

?  Math.ceil(from)

: Math.floor(from);

if (from < 0)

from += len;


for (; from < len; from++)

{

if (from in this &&

this[from] === elt)

return from;

}

return -1;

};

}

Example

Try the following example.

<html>

<head>

<title>JavaScript Array indexOf Method</title>

</head>

<body>

<script type="text/javascript">

if (!Array.prototype.indexOf)

{

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

{

var len = this.length;
  
var from = Number(arguments[1]) || 0;

from = (from < 0)

? Math.ceil(from)

:  Math.floor(from); if (from < 0)

from += len;


for (; from < len; from++)

{

if (from in this &&

this[from] === elt)

return from;

}

return -1;

};

}


function printBr(element, index, array) { document.write("<br />[" + index + "] is " + element );

}


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


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

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

</script>

</body>

</html>

Output

index is : 2

index is : -1

join ()

Javascript array join() method joins all the elements of an array into a string.

Syntax

Its syntax is as follows:

array.join(separator);

Parameter Details

separator : Specifies a string to separate each element of the array. If omitted, the array elements are separated with a comma.

Return Value

Returns a string after joining all the array elements.

Example

Try the following example.

<html>

<head>

<title>JavaScript Array join Method</title>

</head>

<body>

<script type="text/javascript">


var arr = new Array("First","Second","Third");


var str = arr.join();

document.write("str : " + str );


var str = arr.join(", "); document.write("<br />str : " + str );


var str = arr.join(" + ");

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

</script>

</body>

</html>

Output


str : First,Second,Third

str : First, Second, Third

str : First + Second + Third


0 comments:

Post a Comment