
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']
标签: es6语法, indexOf, splice, 数组替换
上面是“es6语法如何替换数组中指定元素”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_2785.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

新手建站最应该注意的是三个关键词匹配?
Windows电脑如何获取物理硬盘序列号(具体操作步骤)
LLM4AD:一个开源、简洁、模块化的基于大模型的自动算法设计平台