数组的对象是Array()
1、length属性:获取字符串的长度
2、一维数组 [1,2,3,4,5,5]
二维数组 [1,2,4,4,5,[5,6,7,7,8]]
var arr = [1,2,4,4,5,[5,6,7,7,8]]; console.log(arr[1]) console.log(arr[5][1])
多维数组 [1,2,4,4,5,[5,6,7,[5,6,7,7,8],7,8]];
3、新增和删除
push():向数组的末尾新增一个或多个元素
var arr = [1,2,3,4,5]; arr.push(10,11); console.log(arr)
pop():从数组的末尾移除元素
var arr = [1,2,3,4,5]; arr.pop(); console.log(arr)
unshift():向数组的头部新增一个或多个元素
var arr = [1,2,3,4,5]; arr.unshift(10,11,12); console.log(arr)
shift():从数组的头部移除元素
var arr = [1,2,3,4,5]; arr.shift(); console.log(arr)
splice():可以用来新增和删除数组的数据,
splice(index,0,val,val....):从数组下标index位置开始新增N个元素
var arr = [1,2,3,4,5]; arr.splice(2,0,10,11,12); console.log(arr)
splice(index,num):从数组下标index位置开始删除num个元素
var arr = [1,2,3,4,5]; var arr2 = arr.splice(2,2); console.log(arr) console.log(arr2)
splice(index,num,val,val1....):从数组下标index位置开始删除num个元素,并新增N个元素
var arr = [1,2,3,4,5]; var arr2 = arr.splice(2,2,10,11,12,13); console.log(arr) console.log(arr2)
注意事项:影响的都是原数组,删除后的元素组成新数组并返回
4、排序
sort():
1、如果不传值的情况下,采用ASCII排序
var arr = [1,11,23,24,2,22,3,44,66,1111];
console.log(arr.sort()) ==>1, 11, 1111, 2, 22, 23, 24, 3, 44, 66
2、升序:
格式:
数组.sort(function(a,b){
return a-b;
})
例如:
var arr = [1,11,23,24,2,22,3,44,66,1111];
console.log(arr.sort(function(a,b){
return a-b;
}))
3、降序
格式:
数组.sort(function(a,b){
return b-a;
})
例如:
var arr = [1,11,23,24,2,22,3,44,66,1111];
console.log(arr.sort(function(a,b){
return b-a;
}))
reverse():倒序
var arr = [1,11,23,24,2,22,3,44,66,1111];
console.log(arr.reverse())
5、元素位置的查找
indexOf():从前向后查找元素在数组中位置,存在则返回下标,不存在则返回-1
lastIndexOf():从后向前查找元素在数组中位置,存在则返回下标,不存在则返回-1
includes():查找元素在数组中的位置,存在则返回true,不存在则返回false
例如:
var arr = [1,11,23,23,24,2,22,3,44,66,1111]; console.log(arr.indexOf(23)) console.log(arr.lastIndexOf(23)) console.log(arr.includes(23)) console.log(arr.includes(123))
//写一个方法,实现一维数组的去重,并通过方法来实现升序或降序
var arr = [1,11,23,23,24,2,22,22,2,3,44,66,1111]; function paixu(arr,paixu = 'asc'){ var newarr = []; for(var i = 0;i<arr.length;i++){ if(!newarr.includes(arr[i])){ newarr.push(arr[i]) } } switch(paixu){ case 'asc': newarr.sort(function(a,b){ return a-b; }) break; case 'desc': newarr.sort(function(a,b){ return b-a; }) break; default: break; } return newarr; } var arr2 = paixu(arr,'desc'); console.log(arr2)
6、Array.isArray():检查变量是否为数组
var arr = [1,11,23,23,24,[1,11,23,23,24,2,22,22,2,3,44,66,111,1111],2,22,22,2,3,44,66,1111]; function unique(arr){ var newArr = []; for(var i = 0;i<arr.length;i++){ if(Array.isArray(arr[i])){ for(var j = 0;j<arr[i].length;j++){ if(!newArr.includes(arr[i][j])){ newArr.push(arr[i][j]) } } }else{ if(!newArr.includes(arr[i])){ newArr.push(arr[i]) } } } return newArr; } var arr2 = unique(arr); console.log(arr2)
7、join():将数组转换成字符串
var arr = [1,11,23,23,24,2,22,22,2,3,44,66,1111]; console.log(arr.join())
8、循环方法:
forEach():
格式:
/**
* item:数组的值
* index:数组的下标
*/
数组.forEach(function(item,index){
})
例如:
var goods = [ { title:'小米手机', price:1999, num:1, totalprice:1999 }, { title:'苹果手机', price:6999, num:1, totalprice:6999 } ]; var totalprice = 0; goods.forEach(function(item,index){ totalprice +=parseFloat(item.totalprice) }) console.log(totalprice)
every():需要数组中每一项都满足条件返回true,否则返回false
格式:
数组.every(function(值){
return 比较
})
var arr = [1,11,23,23,24,2,22,22,2,3,44,66,1111]; console.log(arr.every(function(item){ return item > 10; })) console.log(arr.every(function(item){ return item >= 1; }))
some():需要数组中有一项满足条件则返回true,否则返回false
格式:
数组.some(function(值){
return 比较
})
var arr = [1,11,23,23,24,2,22,22,2,3,44,66,1111]; console.log(arr.some(function(item){ return item > 1000; })) console.log(arr.some(function(item){ return item >= 10000; }))
filter():返回满足条件的元素组成的新数组
格式:
数组.filter(function(值){
return 比较
})
var goods = [ { title:'小米手机', price:1999, num:1, totalprice:1999 }, { title:'苹果手机', price:6999, num:1, totalprice:6999 }, { title:'华为手机', price:2999, num:1, totalprice:6999 }, { title:'Oppo手机', price:1999, num:1, totalprice:6999 } ]; var arr = goods.filter(function(item){ return item.price >2000; }) console.log(arr)
map():返回计算后的元素组成的新数组
var arr = [1,11,23,23,24,2,22,22,2,3,44,66,1111]; var arr2 = arr.map(function(item){ return item * item; }) console.log(arr2)
for in:枚举对象的键
var arr = [1,11,23,23,24,2,22,22,2,3,44,66,1111]; for(a in arr){ console.log(a) }
for of:枚举对象的值
var arr = [1,11,23,23,24,2,22,22,2,3,44,66,1111]; for(a of arr){ console.log(a) }