数组中常用的属性
shift:删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined
var arr1 = [1,2,3,4,5];
var arr2 = [1,2,3,4,5];
var result = arr1.shift(); //arr1 [2,3,4,5] result:1
console.log(result)
unshift:将参数添加到原数组开头,并返回数组的长度
arr1 = [1,2,3,4,5];
result = arr1.unshift(-2,-1); //arr1:[-2,-1,1,2,3,4,5] result:7
console.log(result)
pop:删除原数组最后一项,并返回删除元素的值;如果数组为空则返回undefined
arr1= [1,2,3,4,5];
result = arr1.pop(); //a:[1,2,3,4] b:5
console.log(result)
push:将参数添加到原数组末尾,并返回数组的长度
arr1 = [1,2,3,4,5];
result = arr1.push(6,7); //a:[1,2,3,4,5,6,7] b:7
console.log(result)
*******************************************************************************/
数组合并concat
/concat:返回一个新数组,是将参数添加到原数组中构成的
arr1 = [1,2,3,4,5];
result = arr1.concat(6,7); //a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]
console.log(result)
******************************************************************************
排序 sort 从小到大 reverse 从大到小
// sort(orderfunction):按指定的参数对数组进行排序
arr1 = [1,2,3,4,5];
result = arr1.sort(); //[1,2,3,4,5]
console.log(result)
// reverse:将数组反序
arr1 = [1,2,3,4,5,8,34];
result = arr1.reverse(); //[34,8,5,4,3,2,1]
console.log(result)
/******************************************************************************
slice(start,end) 数组截取
//slice(start,end):返回从原数组中指定开始下标到结束下标之间的项组成的新数组,但不包括结束位置的项
arr1 = [1,2,3,4,5];
result = arr1.slice(2,5); //[3,4,5]
result = arr1.slice(2); //[3,4,5] 在只有一个参数的情况下,slice()方法返回从该参数指定位置开始到当前数组末尾的所有项;
result = arr1.slice(0); //返回自身
/******************************************************************************
join(separator) 数组拼接成字符串
//将数组的元素组起一个字符串,以separator为分隔符,省略的话则用默认用逗号为分隔符
arr1 = [1,2,3,4,5];
result = arr1.join("|"); //a:[1,2,3,4,5] b:"1|2|3|4|5"
/******************************************************************************
splice
//splice(start,deleteCount,val1,val2,...):
// 从start位置开始删除deleteCount项,并从该位置起插入val1,val2,...
arr1 = [1,2,3,4,5];
result = arr1.splice(2,2,7,8,9); //a:[1,2,7,8,9,5] b:[3,4]
result = arr1.splice(0,1); //同shift
arr1.splice(0,0,-2,-1);
result = arr1.length; //同unshift
result = arr1.splice(arr1.length-1,1); //同pop
arr1.splice(arr1.length,0,6,7);
result = arr1.length; //同push