1.数组里可以存一个或者多个变量,数组的表达式
1-1 let arr=["Saab","Volvo","BMW"];
1-2 let arr=new Array("Saab","Volvo","BMW");
2.Array 实例自带的方法
2-1 Array.isArray()
判断是否是数组
/*
let arr=['苹果','橘子','香蕉'];
console.log(Array.isArray(arr))//true
*/
2-2 Array.from();
把类数组的东西转换成数组
let str='hhell';
console.log(Array.from(str));//["h", "h", "e", "l", "l"]
2-3 Array.of();
把一组值转换成数组
console.log(Array.of(1,3,4)); //[1,,3,4]
2.数组的属性
2-1 索引值
根据不同的索引,获取不同的值
2-2 length
获取数组的长度
3.数组的方法
3-1 concat
/*
连接两个数组,返回一个新的数组
*/
let arr1=['饼干']
let arr=['苹果','橘子','香蕉'];
let arr3=arr.concat(arr1);
console.log(arr3) //["苹果", "橘子", "香蕉", "饼干"]
3-2 fill
指定某个元素填充数组,并静态的改变他
let arr=['苹果','橘子','香蕉'];
let arr3=arr.fill('123');
console.log(arr,arr3);//['123','123','123'] ,['123','123','123']
3-3 filter
筛选数组,筛选出年龄大于20的数组
let arr=[{name:'小明',age:20},{name:'小绿',age:21},{
name:'小方',age:23
}];
let res=arr.filter((item)=>item.age>20)
console.log(res)//{name:'小绿',age:21},{name:'小方',age:23}
3-3 find
查找第一个符合条件的属性
let arr=[{name:'小明',age:20},{name:'小绿',age:21},{
name:'小方',age:23
}];
let res=arr.find((item)=>item.age>20)
console.log(res)//{name:'小绿',age:21}查找第一份符合条件的属性
3-4 findIndexof
let arr=[{name:'小明',age:20},{name:'小绿',age:21},{
name:'小方',age:23
}];
let res=arr.findIndex((item)=>item.age>20)
console.log(res)//查找第一个符合条件的属性数组的索引
3-5 forEach遍历数组
let arr=[{name:'小明',age:20},{name:'小绿',age:21},{
name:'小方',age:23
}];
arr.forEach((item)=>{
console.log(item)
})
3-6 includes
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.includes("Mango"))//查找第一个符合条件的属性数组 的//true
3-7 indexOf
查找是否含有这一项的索引值
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
console.log(a)//2
3-8 join
let data = ["2021", "11", "5"];
let data_string = data.join('/');
console.log(data_string) 2021/11/5
3-9 keys
var fruits = [{
name:'333',
},{
name:'444'}]
var fk = fruits.keys();
for (x of fk) {
console.log(x)
}
//0,1 返回的是数组的索引值
3-10 map 可以筛选部分元素
var numbers = [{name:'小明'},{name:'小绿'},{name:'消耗欧诺个'}];
var x = numbers.map((item)=>{
let obj={
name:item.name
}
return obj
});
console.log(x) //[{name: "小明"},{name: "小绿"} ,{name: "消耗欧诺个"}]
3-11 splice
array.splice(index,howmany,item1, .....,itemX); //index 那个位置 删除几个元素 追加几个元素
3-12 push
从数组的末尾追加元素
arr.push()
3-13 toString()
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.toString();
document.getElementById("demo").innerHTML = text; //Banana, Orange, Apple, Mango
3-13 sort 排序 从小到大排序
var arrayValue=new Array(10,5,4,30,100);
arrayValue.sort(function(a,b){return a-b;});
console.log(arrayValue)//4, 5, 10, 30, 100 从小到大排序