join的方法
将数组拼接成字符串
var arr = ["html","css","js"];
// join可以将数组拼接成一个字符
console.log(arr.join("$"))
console.log(arr.join("|"))
console.log(arr.join(" "))
console.log(arr.join("*"))
console.log(typeof arr.join(""))
2sort
arr.sort((a,b)=>a-b);
arr.sort((a,b)=>b-a);
3求和reduce
特点:只能对简单的值求和[1,2,3]
4,reverse
特点:能够改变数组的结构
5遍历
var arr = ["html","css","js"];
arr.reverse();
console.log(arr);
for in方法
//for in
var arr = ["html","css","js"];
for(var key in arr){
console.log(key)//key获取的是数组的下标
//console.log(arr[key])
}
var obj = {
name:"list",
age:18
}
for(var k in obj){
console.log(obj[k])
}
获取下标的方法
var arr =[3,4,5];
for(var value of arr){
console.log(value)
//获取下标怎么获取
//indexOF
console.log(arr.indexOf(value))
}
for each方法
var arr = ["html","css","js"];
arr.forEach(function(item){
console.log(item)
})
方法2
var arr = [2,3,4];
var newArr = [];
arr.forEach(item=>{
console.log(item)
newArr.push(item*2)
})
console.log(newArr)
map遍历
var arr = [
{name:"html",value:"12px"},
{name:"css",value:"13px"},
{name:"js",value:"14px"}
]
arr.map(item=>{
var value = parseInt(item.value)*2;
item.value = value;
})
console.log(arr)
some 遍历 数组中有一项满足某个条件时 就返回true
var arr = [3,4,5,6];
var res = arr.some(item=>{
return item>1;
})
console.log(res);
every遍历 数组中每一项都要满足条件 才返回true
var arr = [2,3,4];
var res = arr.every(item=>{
return item>1;
})
console.log(res)
filter遍历 只要达到某个条件 就会返回一个新的数组
var arr = [3,4,5,6];
// filter 只要达到某个条件 就会返回一个新的数组
var res = arr.filter(item=>{
return item>4;
})
console.log(res)
find 遍历 返回数组中满足条件的某个值 只返回一个值
var arr=[2,3,4,3];
var res= arr.find(item=>{
return item==3;
})
console.log(res)
findIndex遍历 返回数组中满足条件的数组的下标
var arr =[2,3,4];
// findIndex 返回数组中满足条件的值的下标
var index = arr.findIndex(item=>{
return item == 4;
})
console.log(index)
<input type="text" id="app">
<script>
var arr = [
{name:"李四",id:"1001"},
{name:"王四",id:"1002"},
{name:"张四",id:"1003"},
{name:"赵四",id:"1004"}
]
/*
1.要获取数组中的值
2.根据id值 获取数组中对应项的下标
3.删除对应的项
*/
var app = document.getElementById("app");
app.onkeydown = function(event){
if(event.keyCode==13){
var id =Number(this.value);
var index =arr.findIndex(item=>{
return item.id == id;
})
arr.splice(index,1);
console.log(arr)
}
}
</script>