Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JavaScript array replace element 
 

In the JavaScript array there is not any in-built replace() methods for replacing elements but we can implement replace() method by using the in-built splice() method.

 

JavaScript array replace element

                         

In the JavaScript array there is not any in-built replace() methods for replacing elements but we can implement replace() method by using the in-built splice() method. We have already discussed the splice() method in our JavaScript array tutorial. For implementation of replace() method we have created a replace() method which takes three arguments as given below:

  • arrayName: Array Object at which replacement is to be done. 
  • repalceTo: array element to which we have to replace in the array 
  • replaceWith: array element against which replacement is to be done

 

 

 

function replace(arrayName,replaceTo, replaceWith)
{
  for(var i=0; i<arrayName.length;i++ )
  {  
    if(arrayName[i]==replaceTo)
      arrayName.splice(i,1,replaceWith);          
  }        
}

Above code is the function defined for implementing replace() method. 

replace(arr,"Suman","Vineet"); Here we have passed "Vineet" to be replaced by the array element "Suman".

Full code of javascript_array_replace.html is as follows:

<html>
  <head>
  <title>
       JavaScript array replace
    </title>
   <script type="text/javascript">
   var arr = new Array(5);
  arr[0]="Sandeep";
  arr[1]="Suman";
  arr[2]="Saurabh";
  arr[3]="Vinod";
  arr[4]="Amar";

function replace(arrayName,replaceTo, replaceWith)
{
  for(var i=0; i<arrayName.length;i++ )
  {  
    if(arrayName[i]==replaceTo)
      arrayName.splice(i,1,replaceWith);          
  }        
}
  document.write("<b>Before Replacement</b>=>"+arr+"<br>");
     replace(arr,"Suman","Vineet");
  document.write("<b>After Replacement</b>=>"+arr+"<br> ");
</script>
</head>
<body bgcolor="#ffffdd">
  <h2>
    <font color="blue">
      Example of implementing replace()<br>
      method on JavaScript Array
    </font>
  </h2>
</body>
</html>

Output of the example is as follows:

Download Sample Source Code

                         

» View all related tutorials
Related Tags: java javascript c string array join script object method ip using int element elements to ld generate e can put

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.