How to replace an item in an array

We may face this situation a lot in Javascript development. So the below is the simply solution to replace an item in an array with splice() method.

// replace France to Italia
var countries = ['USA', 'France', 'England'];  
countries.splice(1, 1, 'Italia');  
console.log(countries);  
// ["USA", "Italia", "England"]

You can find out more about array's method in this post.

Nguyen Tran

I'm Nguyen, a full stack web passion for frontend and love Angular.

Subscribe to Nguyen Tran

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!