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

nodejs如何利用Server-Sent Events实现实时发送数据
vue-count-to:一个无依赖、轻量级的Vue组件,专门用于实现数字滚动计数效果 











