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
Release: 2023-06-01 14:25:06
forward
1507 people have browsed it

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']
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template