The splice() method adds/removes items to/from the array and returns the deleted item. This method mutates the original array. Pay special attention to where you insert the value!
// arrayObject.splice(index,howmany,item1,.....,itemX) var plants = ['Saturn', 'Uranus', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter']; var result = plants.splice(2, 1, 'www.shanzhonglei.com') console.log(plants); // ['Saturn','Uranus','www.shanzhonglei.com','Mercury','Venus','Earth','Mars','Jupiter'] console.log(result); // ['Mercury']
The above is the detailed content of How to replace specific values in an array in Javascript. For more information, please follow other related articles on the PHP Chinese website!