💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[toc] ### 1. map 映射,一对一,将每一项的item都执行一遍函数 ``` let arr = [1, 2, 3, 4]; arr.map(function(item){ item *= 2; console.log(item); // 2, 4, 6, 8 }) let result = arr.map((item)=>item*2); console.log(result); //[2, 4, 6, 8] ``` ### 2. reduce 汇总,多变一 ``` arr.reduce(function(tmp, item, index){} ``` tmp从第一项开始,tmp和item执行函数,之后tmp接受返回值,item从第二项开始,index为item的下标 ``` /* 计算平均数 */ let arr = [1, 2, 3, 4]; let result = arr.reduce(function(tmp, item, index){ if(index != arr.length-1) { return tmp+item; }else{ return (tmp+item)/arr.length; } }); console.log(result) //2.5 ``` ### 3. filter 过滤器 ``` let arr = [ {title: "男士衬衫", price: 75}, {title: "女士包", price: 11000}, {title: "男士包", price: 65}, {title: "女士鞋", price: 13000} ]; let result = arr.filter(function(json){ return json.price >= 10000; }) console.log(result); //[{…}, {…}] ``` ### 4. forEach 迭代 ``` arr.forEach(function(item, index)){ } ``` ### 5. find()&findIndex() ``` stu.find((element) => (element.name == '李四')); //返回的是{name: "李四", gender: "男", age: 20}这个元素 stu.findIndex((element)=>(element.name =='李四')); //返回的是索引下标:2 ```