join的方法

  1. 将数组拼接成字符串
  2. var arr = ["html","css","js"];
  3. // join可以将数组拼接成一个字符
  4. console.log(arr.join("$"))
  5. console.log(arr.join("|"))
  6. console.log(arr.join(" "))
  7. console.log(arr.join("*"))
  8. console.log(typeof arr.join(""))

2sort

  1. arr.sort((a,b)=>a-b);
  2. arr.sort((a,b)=>b-a);

3求和reduce

  1. 特点:只能对简单的值求和[1,2,3]

4,reverse

  1. 特点:能够改变数组的结构

5遍历

  1. var arr = ["html","css","js"];
  2. arr.reverse();
  3. console.log(arr);

for in方法

  1. //for in
  2. var arr = ["html","css","js"];
  3. for(var key in arr){
  4. console.log(key)//key获取的是数组的下标
  5. //console.log(arr[key])
  6. }
  7. var obj = {
  8. name:"list",
  9. age:18
  10. }
  11. for(var k in obj){
  12. console.log(obj[k])
  13. }

获取下标的方法

  1. var arr =[3,4,5];
  2. for(var value of arr){
  3. console.log(value)
  4. //获取下标怎么获取
  5. //indexOF
  6. console.log(arr.indexOf(value))
  7. }

for each方法

  1. var arr = ["html","css","js"];
  2. arr.forEach(function(item){
  3. console.log(item)
  4. })

方法2

  1. var arr = [2,3,4];
  2. var newArr = [];
  3. arr.forEach(item=>{
  4. console.log(item)
  5. newArr.push(item*2)
  6. })
  7. console.log(newArr)

map遍历

  1. var arr = [
  2. {name:"html",value:"12px"},
  3. {name:"css",value:"13px"},
  4. {name:"js",value:"14px"}
  5. ]
  6. arr.map(item=>{
  7. var value = parseInt(item.value)*2;
  8. item.value = value;
  9. })
  10. console.log(arr)

some 遍历 数组中有一项满足某个条件时 就返回true

  1. var arr = [3,4,5,6];
  2. var res = arr.some(item=>{
  3. return item>1;
  4. })
  5. console.log(res);

every遍历 数组中每一项都要满足条件 才返回true

  1. var arr = [2,3,4];
  2. var res = arr.every(item=>{
  3. return item>1;
  4. })
  5. console.log(res)

filter遍历 只要达到某个条件 就会返回一个新的数组

  1. var arr = [3,4,5,6];
  2. // filter 只要达到某个条件 就会返回一个新的数组
  3. var res = arr.filter(item=>{
  4. return item>4;
  5. })
  6. console.log(res)

find 遍历 返回数组中满足条件的某个值 只返回一个值

  1. var arr=[2,3,4,3];
  2. var res= arr.find(item=>{
  3. return item==3;
  4. })
  5. console.log(res)

findIndex遍历 返回数组中满足条件的数组的下标

  1. var arr =[2,3,4];
  2. // findIndex 返回数组中满足条件的值的下标
  3. var index = arr.findIndex(item=>{
  4. return item == 4;
  5. })
  6. console.log(index)
  1. <input type="text" id="app">
  2. <script>
  3. var arr = [
  4. {name:"李四",id:"1001"},
  5. {name:"王四",id:"1002"},
  6. {name:"张四",id:"1003"},
  7. {name:"赵四",id:"1004"}
  8. ]
  9. /*
  10. 1.要获取数组中的值
  11. 2.根据id值 获取数组中对应项的下标
  12. 3.删除对应的项
  13. */
  14. var app = document.getElementById("app");
  15. app.onkeydown = function(event){
  16. if(event.keyCode==13){
  17. var id =Number(this.value);
  18. var index =arr.findIndex(item=>{
  19. return item.id == id;
  20. })
  21. arr.splice(index,1);
  22. console.log(arr)
  23. }
  24. }
  25. </script>