引用方法操作方法
Posted xiaoxustudy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了引用方法操作方法相关的知识,希望对你有一定的参考价值。
1 <script> 2 var arr1 = ["a","b","c",undefined,null]; 3 //concat() 4 //创建数组副本,将接收到的参数添加到副本末尾 5 var arr2 = arr1.concat("d","e"); 6 console.log(arr1); //输出:(5) ["a", "b", "c", undefined, null] 7 console.log(arr2); //输出:(7) ["a", "b", "c", undefined, null, "d", "e"] 8 9 //slice() 10 //返回指定位置的项 11 //接收1个参数 12 console.log(arr1.slice(1)); //输出:(4) ["b", "c", undefined, null] 13 //接收2个参数 14 console.log(arr1.slice(1,3)); //输出:(2) ["b", "c"] 15 //参数为负数。用数组长度加上该数来确定相应的位置 16 console.log(arr1.slice(-4,-2)); //输出:(2) ["b", "c"] 17 18 //splice() 19 //删除任意数量的项 20 //splice(要删除的第一项的位置,要删除的项数) 21 var removed = arr1.splice(3,2); 22 console.log(removed); //输出:(2) [undefined, null] 23 console.log(arr1); //输出:(3) ["a", "b", "c"] 24 //插入任意数量的项 25 //splice(起始位置,要删除的项数,要插入的项) 26 var inserted = arr1.splice(1,0,"insert"); 27 console.log(inserted); //输出:[] 28 console.log(arr1); //输出:(4) ["a", "insert", "b", "c"] 29 var inserted2 = arr1.splice(1,2,"insert2"); 30 console.log(inserted2); //输出:(2) ["insert", "b"] 31 console.log(arr1); //输出:(3) ["a", "insert2", "c"] 32 //向指定位置插入任意数量的项,同时删除任意数量的项 33 //splice(起始位置,要删除的项数,要插入的任意数量的项) 34 var instead = arr1.splice(1,2,"b"); 35 console.log(instead); //输出:(2) ["insert2", "c"] 36 console.log(arr1); //输出:(2) ["a", "b"] 37 </script>
以上是关于引用方法操作方法的主要内容,如果未能解决你的问题,请参考以下文章