前言
我是歌谣 歌谣的意志是永恒的 放弃很容易 但是坚持一定很酷
导语
前端数组方法splice的使用
编辑
代码部分
//splice(start,end,index1,index2) 从什么位置开始,删几个?删了后插入什么新元素? var geyao=["1","2","3","4"] var geyaotest=["1","2","3","4"] var geyaofangfang=["1","2","3","4"] var fangfang=["1","2","3","4"] var fangfangTest=["1","2","3","4"] var fangfangBang=["1","2","3","4"] var kangkang=["1","2","3","4"] var kangkangni=["1","2","3","4"] var kangkanghao=["1","2","3","4"] //1个参数会截取当前 大于2之后的数都会被删除 geyao.splice(2) console.log(geyao,"geyao")//["1","2"] //2个参数 表示从什么位置截取到什么位置 把下标为2的截取掉 geyaotest.splice(2,1) console.log(geyaotest,"geyaotest") //[ "1", "2", "4" ] //3个参数 在什么位置到什么位置添加 geyaofangfang.splice(2,1,"5") console.log(geyaofangfang,"geyaofangfang") //[ "1", "2", "5", "4" ] //超出数组长度 默认获得前面的全部 fangfang.splice(5) console.log(fangfang,"fangfang") // [ "1", "2", "3", "4" ] //从5之后开始截取 fangfangTest.splice(5,1) console.log(fangfangTest,"fangfangTest") // [ "1", "2", "3", "4" ] //对后置的位置进行添加 fangfangBang.splice(5,1,"6") console.log(fangfangBang,"fangfangBang") // [ "1", "2", "3", "4","6"] //倒着开始截取剩余部分 kangkang.splice(-1) console.log(kangkang,"kangkang") // [ "1", "2", "3" ] //倒着进行处理 kangkangni.splice(-2,1) console.log(kangkangni,"kangkangni")//[ "1", "2", "4" ] //倒着插入对应值 kangkanghao.splice(-2,1,"8") console.log(kangkanghao,"kangkanghao")//[ "1", "2", "8", "4" ] //该方法向或者从数组中添加或者删除项目,返回被删除的项目。(该方法会改变原数组)
总结
//该方法向或者从数组中添加或者删除项目,返回被删除的项目。(该方法会改变原数组)