首页 > 代码库 > Array[数组]常用属性与方法

Array[数组]常用属性与方法

index:从0开始的数组下标;

length:数组长度;

prototype:数组原型属性;

数组常用方法

concat:连接数组;

示例: arr1 = [1,2,3];  arr2 = [4,5,6];  arr1.concat(arr2) = [1,2,3,4,5,6];

join():将数组转换为字符串,()里参数为连接字符,默认为",";

示例:arr1 = [‘a‘,‘b‘,‘c‘];      arr1.join() = a,b,c ;  arr1.join(‘ ‘) = a b c;  arr1.join(‘ + ‘) = a + b + c ;

pop():移除数组的最后一个元素

示例:arr1 = [1,2,3,4]       arr1.pop() ==> arr1 =  [1,2,3];arr1.pop()=4

push():将括号内元素追加到原数组后面

示例:arr1 = [1,2,3,4]      arr1.push(3,2,1)  ==>  arr1 = [1,2,3,4,3,2,1];

reverse():倒序数组

示例:arr1 = [1,2,3]    arr1.reverse() = [3,2,1];

shift():移除数组的第一个元素

示例:arr1=[‘a‘,‘b‘,‘c‘];       arr1.shift()  ==> arr1 = [‘b‘,‘c‘];arr1.shift() = c;

unshift():将括号内元素添加到原数组前面

示例:arr1 = [1,2,3,4]      arr1.unshift(3,2,1)  ==>  arr1 = [3,2,1,1,2,3,4];

splice( , ,..):移除数组里的元素==>第一个参数为起始下标,第二个参数为要移除元素的个数,第三个及以后参数为替换元素--取代被移除的元素

示例:arr1 = [1,2,3,4]     arr1.slice(1,2);==>arr1 = [1,4] ; //  arr1.slice(1,2,‘a‘,‘b‘);==>arr1 = [1,‘a‘,‘b‘,4];

slice(start,end):切割数组    start:开始下标   end:结束下标    结果 [start,end),原数组不发生变化

示例:arr1= [1,2,3,4]  arr2 = arr1.slice(1,3) ==>arr1 = [1,2,3,4];  arr2 = [2,3];

toString():将数组转换为字符串格式,以","连接

示例:arr1 = [‘a‘,‘b‘,‘c‘];  arr1.toString() = a,b,c;

 

Array[数组]常用属性与方法