2025年02月26日 建站教程
es6语法如何利用indexOf()和Array.splice()替换数组中指定元素,下面web建站小编给大家详细介绍一下!
利用indexOf()替换数组中指定元素
const arr = ['a', 'b', 'c'];
const index = arr.indexOf('a'); // ?️ 0
if (index !== -1) {
arr[index] = 's';
}
console.log(arr);
//(3) ['s', 'b', 'c']
利用Array.splice()替换数组中指定元素
const arr = ['a', 'b', 'c'];
const index = arr.indexOf('b'); // ?️ 1
arr.splice(index, 1, 'h');
console.log(arr);
//['a', 'h', 'c']
本文链接:http://so.lmcjl.com/news/23882/