js 数组操作常用方法

code参考了: https://www.cnblogs.com/woshidouzia/p/9304603.html


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//测试数组
    let arr = [1,2,3,4,5,6,7]
 
    console.log("=============1.for循环=============")
    for(let j=0, len = arr.length; j<len;j++){
      console.log(arr[j])
    }
    console.log("=============2.forEach循环=============")
    arr.forEach((item,index,array)=>{
      console.log(item,index,array)
    })
    console.log("=============3.map循环=============")
    //map的回调函数中支持return返回值;return的是啥,相当于把数组中的这一项变为啥
    // (并不影响原来的数组,只是相当于把原数组克隆一份,把克隆的这一份的数组中的对应项改变了);
    arr.map(function(value,index,array){
      console.log("map循环"+index+array)
      return 2*value;
 
    })
    console.log("=============4.forof遍历=============")
    for (var value of arr){
      console.log("forof遍历"+value)
    }
    console.log("=============5.filter遍历=============")
    var arr1 = [
      { id: 1, text: 'aa', done: true },
      { id: 2, text: 'bb', done: false }
    ]
    console.log(arr1.filter(item => item.done))
 
    console.log("=============6.every遍历=============")
    //every()是对数组中的每一项运行给定函数,如果该函数对每一项返回true,则返回true。
    var arr2 = [ 1, 2, 3, 4, 5, 6 ];
    console.log( arr2.every( function( item, index, array ){
      console.log("every遍历" + index + array)
      return item > 3;
    }));
 
    console.log("=============7.some遍历=============")
    //some()是对数组中每一项运行指定函数,如果该函数对任一项返回true,则返回true。
    var arr3 = [ 1, 2, 3, 4, 5, 6 ];
    console.log( arr3.some( function( item, index, array ){
      console.log("some遍历" + index + array)
      return item > 3;
    }));
 
    console.log("=============8.reduce() =============")
    //reduce()方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值。
    arr.reduce(function(previousValue, currentValue, index, array){
      console.log("reduce遍历" + index + array)
      return previousValue + currentValue;
    });
    console.log("=============9.reduce 还有第二个参数=============")
    //我们可以把这个参数作为第一次调用callback时的第一个参数,上面这个例子因为没有第二个参数,所以直接从数组的第二项开始,如果我们给了第二个参数为5,那么结果就是这样的:
    arr.reduce(function(previousValue, currentValue, index, array){
      console.log("reduce遍历" + index + array)
      return previousValue + currentValue;
    },5);
 
    console.log("=============10.reduceRight()=============")
    //方法的功能和reduce()功能是一样的,不同的是reduceRight()从数组的末尾向前将数组中的数组项做累加。
    var arr4 = [0,1,2,3,4];
 
    arr4.reduceRight(function (preValue,curValue,index,array) {
      console.log("reduceRight"+index+array)
      return preValue + curValue;
    });
 
    console.log("=============11.find=============")
    //find()方法返回数组中符合测试函数条件的第一个元素。否则返回undefined
    var stu = [
      {
        name: '张三',
        gender: '男',
        age: 20
      },
      {
        name: '王小毛',
        gender: '男',
        age: 20
      },
      {
        name: '李四',
        gender: '男',
        age: 20
      }
    ]
    console.log("find"+stu.find((element) => (element.name == '李四')))
 
    console.log("=============12.findIndex=============")
    //对于数组中的每个元素,findIndex 方法都会调用一次回调函数(采用升序索引顺序),直到有元素返回 true。只要有一个元素返回 true,findIndex 立即返回该返回 true 的元素的索引值。
    // 如果数组中没有任何元素返回 true,则 findIndex 返回 -1。
    var arr5 = [1,2,3]
    console.log(arr5.findIndex(function(x) { x == 2; }))
    console.log(arr5.findIndex(x => x == 4))
 
    console.log("=============keys,values,entries=============")
    //ES6 提供三个新的方法 —— entries(),keys()和values() —— 用于遍历数组。
    // 它们都返回一个遍历器对象,可以用for...of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历
    for (let index of ['a', 'b'].keys()) {
      console.log(index);
    }

 

posted @   liuw_flexi  阅读(164)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· 2025成都.NET开发者Connect圆满结束
· langchain0.3教程:从0到1打造一个智能聊天机器人
点击右上角即可分享
微信分享提示