Home >Operation and Maintenance >Safety >How to replace specific values ​​in an array in Javascript

How to replace specific values ​​in an array in Javascript

WBOY
WBOYforward
2023-06-01 14:25:061510browse

Replace specific values ​​in the array

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete