标签:length let div 清空 style unshift 区别 长度 ons
1、创建数组
2、增
1)push , unshift
2)splice
var arr3 = [1,2,3,4,6]; var result = arr3.splice(2,0,"sdf") //第二个位置,删除0个元素,插入值 console.log(arr3) // [1, 2, "sdf", 3, 4, 6]
3)length
var arr = [1,2]; arr[arr.length] = 3 console.log(arr); // [1, 2, 3]
3、删
1)删除一个:pop, shift
2)splice
var arr2 = [1,2,3,4,6]; var result = arr2.splice(1,3) console.log(result) // [2, 3, 4] 删除的值 console.log(arr2) // [1, 6] 删除后得到的值 ---------------------------------- var arr3 = [1,2,3,4,6]; var result = arr3.splice(1,3,"sdf") console.log(result) // [2, 3, 4] 删除的值 console.log(arr3) // [1, "sdf", 6] 删除后插入的值
3)delete
var arr = [1, 2, 3, 4]; delete arr[0]; console.log(arr); //[undefined, 2, 3, 4] delete删除之后数组长度不变,只是被删除元素被置为undefined了
4)数组属性length
var t = [1,23,5,6,7]; t.length =3; console.log(t) // [1,23,5]
5)清空数组
a、arr.length = 0
b、arr = [ ]
区别: var arr = [1,23,5,6,7]; var arr1 = arr; arr.length = 0; console.log(arr) // [] console.log(arr1) // [] ----------------------------- var arr2 = [1,23,5,6,7]; var arr3 = arr2; arr2 = []; console.log(arr2) // [] console.log(arr3) // [1, 23, 5, 6, 7]
标签:length let div 清空 style unshift 区别 长度 ons
原文地址:https://www.cnblogs.com/yuyedaocao/p/12067038.html