Home Javascript JavaScript Array Remove Element

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript Array Remove Element

Advertisement
In this example we will describe JavaScript Array Remove Element. First of all we have created an array list and store string values to it like "Ramu" etc.

JavaScript Array Remove Element

In this example we will describe JavaScript Array Remove Element. First of all we have created an array list and store string values to it like "Ramu" etc. We have then created JavaScript Function. In this example we have used remove() method to remove element. To remove an element we have to specify its particular position. We have used JavaScript array's Splice() method. Splice method remove an element at the specified index position.

JavaScript Remove function.


function removeByElement(arrayName,arrayElement)
{
for(var i=0; i<arrayName.length;i++ )
{
if(arrayName[i]==arrayElement)
arrayName.splice(i,2);
}
}

Example

<html>
<head>
<title>



JavaScript array remove element example
</title>
<script type="text/javascript">
var arr = new Array(5);
arr[0]="Ramu";
arr[1]="Rahul";
arr[2]="Ankit";
arr[3]="Deepak";
arr[4]="Gopal";

function removeByElement(arrayName,arrayElement)
{
for(var i=0; i<arrayName.length;i++ )
{ 
if(arrayName[i]==arrayElement)
arrayName.splice(i,2); 
} 
}
document.writeln("Array before removeByElement() method is =<b>"+arr+"</b></br>");
removeByElement(arr,"Ankit");
document.writeln("Array after removeByElement() method is =<b>"+arr+"</b></br>");
</script>
</head>
<body>
<h2>
JavaScript array remove element
</h2>
</body>
</html>

OutPut

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 5, 2013

Related Tutorials

Discuss: JavaScript Array Remove Element  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com