重新回头学习归纳ES6的知识点-------数组循环

news/2024/7/10 23:16:02 标签: es6

1.forEach循环

    就是代替普通的for循环,它接受两个参数(循环回调函数,this的指向)

arr.forEach(function(val,index,arr){
    console.log(val,index,arr);
},this);        

2. map

    正常情况下,需要配合return,返回的是一个新数组; 若是没有return,则相当于forEach;  注意:平时只有用map一定要有return,没有就用forEach就可以

    重新整理数据结构:

let arr = [
            {title:'aaaaa', read:100, hot:true},
            {title:'bbbb', read:100, hot:true},
            {title:'cccc', read:100, hot:true},
            {title:'dddd', read:100, hot:true}
        ];
let newArr = arr.map((item,index,arr)=>{
    var json = {};
    json.t = `~~${item.title}----`;
    json.r = item.read/2;
    json.h = item.hot == true && '666;
});
console.log(newArr);

3. filter

    过滤,过滤一些不合格 “元素”;如果回调函数返回true,就留下来; 

let arr = [
   {title:'aaaaa', read:3, hot:true},
   {title:'bbbb', read:4, hot:false},
   {title:'cccc', read:5, hot:true},
   {title:'dddd', read:6, hot:false}
];        
let newArr = arr.filter((item, index, arr)=>{
    return item.read > 4;
});
console.log(newArr);

4. some  

    有点去比对去查找的感觉,数组里面某一个元素符合条件就返回true

5. every

    数组里面所有元素都符合条件才返回true

6. reduce  

    从左向右

let arr = [1,2,3,4,5,6,7,8,9,10];

let res = arr.reduce((prev, cur, index, arr) =>{
    console.log(prev,cur)
    return prev+cur;
});

console.log(res);

5. reduceRight

    从右向左

 


http://www.niftyadmin.cn/n/1496748.html

相关文章

551 Student Attendance Record I 学生出勤纪录 I

给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: A : Absent,缺勤 L : Late,迟到 P : Present,到场如果一个学生的出勤纪录中不超过一个A(缺勤)并且不超过两个连续的L(迟到),那么这个学…

Java线程池相关类-Executor框架

1.Executor 接口源码&#xff1a; public interface Executor {/*** Executes the given command at some time in the future. The command* may execute in a new thread, in a pooled thread, or in the calling* thread, at the discretion of the <tt>Executor<…

重新回头学习归纳ES6的知识点-------promise(承诺)

概述&#xff1a; 是异步编程的一种解决方案。解决异步回调问题。 从语法上说&#xff0c;Promise 是一个对象&#xff0c;从它可以获取异步操作的消息。 特点&#xff1a; 对象的状态不受外界影响。 Promise对象代表一个异步操作&#xff0c;有三种状态&#xff1a;pending&…

Java多线程 5:Thread中的实例方法

Thread类中的方法调用方式&#xff1a;快速到底 学习 Thread 类中的方法是学习多线程的第一步。在学习多线程之前特别提出一点&#xff0c;调用 Thread 中的方法的时候&#xff0c;在线程类中&#xff08;千万别忘记了这个前提条件&#xff09;&#xff0c;有两种方式&#xff…

重新回头学习归纳ES6的知识点---------模块 Module 的语法

1.概述&#xff1a; ES6 模块不是对象&#xff0c;而是通过export命令显式指定输出的代码&#xff0c;再通过import命令输入。 import命令会被 JavaScript 引擎静态分析&#xff0c;先于模块内的其他语句执行&#xff08;import命令叫做“连接” binding 其实更合适&#xff…

7.Web Service 调用天气代码

1. 1 2500多个城市天气预报 WEB服务公用事业2 Endpoint:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx3 Disco:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?disco4 WSDL:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl5 …

HYSBZ - 2038 小Z的袜子(hose) (莫队)

中文题面 题解&#xff1a;莫队组合数&#xff0c;注意LL啊啊啊 #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <map> #include <queue> #include <vector> #include <cstring> #in…

重新回头学习归纳ES6的知识点---------类(class)和继承

JavaScript 语言中&#xff0c;生成实例对象的传统方法是通过构造函数&#xff1b; ES6 提供了更接近传统语言的写法&#xff0c;引入了 Class&#xff08;类&#xff09;这个概念&#xff0c;作为对象的模板。通过class关键字&#xff0c;可以定义类。 基本上&#xff0c;ES…