我学到的一些(JavaScript中的map和filter、reduce方法;ES6新特性:箭头函数)

我学到的(2019.4.21)

声明:本人还是一个大三小白,有错误请谅解。


1.JavaScript中的map和filter、reduce等方法

.forEach(element,index,array)
  遍历数组,参数为一个回调函数,回调函数有三个参数:当前元素,元素索引,整个数组

var a=new Array(1,2,3,4,5,6);
a.forEach(function(e,i,array){
    array[i]=e+1;//array:当前数组,i:当前索引,e:当前元素
});
console.log(a); //[2, 3, 4, 5, 6, 7] 

.map(function(element))
  与forEach类似,遍历数组,回调函数返回值组成一个新数组返回,新数组索引结构和原数组一致,原数组不变

'use strict'//严格模式
 let arr = [1, 2, 3, 4];
  let newArr = arr.map(function(item) {  // 使用map方法
          return item * 2;
  });
  console.log(newArr);    // [2, 4, 6, 8]

.filter(function(element))
返回数组的一个子集,回调函数用于逻辑判断是否返回,返回true则把当前元素加入到返回数组中,false则不加,新数组只包含返回true的值,索引缺失的不包括,原数组保持不变

let arr = [1, 2, 3, 4];
  let newArr = arr.filter(function(item) {  // 使用filter方法
         if (item % 2 !== 0) {
             return item;
         } 
 });
 console.log(newArr);    // [1, 3]

.reduce(function(v1,v2),value) / .reduceRight(function(v1,v2),value)
  遍历数组,调用回调函数,将数组元素累加成一个值,reduce从索引最小值开始,reduceRight反向,
  方法有两个参数:
  1、回调函数:把两个值合为一个,返回结果
  2、value,一个初始值,可选

'use strict'
let arr = [1,2,3,4];
let newArr=arr.reduce(function(a,b){
        return a+b;
        }); 
console.log(newArr) ;

.every(function(element,index,array)) 和 .some(function(element,index,array))
  这两个函数类似于离散数学中的逻辑判定,回调函数返回一个布尔值:
  every是“所有”函数的每个回调函数都返回true的时候才会返回true,当遇到false的时候终止执行,返回false;
  some函数是“存在”有一个回调函数返回true的时候终止执行并返回true,否则返回false。
  在空数组上调用every返回true,some返回false。

var a=new Array(1,2,3,4,5,6);
console.log(a.every(function(e,i,arr){
    console.log(i+' : '+e);
    return e<5;
}));
/*0 : 1
: 2
: 3
: 4
: 5
false */
//如果改成return e<7; 则最终结果是true
var a=new Array(1,2,3,4,5,6);
console.log(a.some(function(e,i,arr){
    console.log(i+' : '+e);
    return e>4;
}));
/*0 : 1
: 2
: 3
: 4
: 5
true */
//如果改成return e>6;则返回false

在这里插入图片描述
filter()、forEach()、map()、some()、every()都是对数组的每一项调用函数进行处理。
区别:
– some()、every()的返回值 :true / false
– filter()、map()的返回值 :一个新数组
– forEach()无返回值。
使用filter()、forEach()、map()、some()、every()都不改变原数组。

参考: https://blog.csdn.net/jiandan1127/article/details/78644968
——— https://blog.csdn.net/b954960630/article/details/81432881

2.ES6新特性:箭头函数

ES6可以使用“箭头”(=>)定义函数,注意是函数,不要使用这种方式定义类(构造器)。
一、语法

  1. 具有一个参数的简单函数
var single = a => a
single('hello, world') // 'hello, world'
  1. 没有参数的需要用在箭头前加上小括号
var log = () => {
    alert('no param')
}
  1. 多个参数需要用到小括号,参数间逗号间隔,例如两个数字相加
var add = (a, b) => a + b
add(3, 8) // 11
  1. 函数体多条语句需要用到大括号
var add = (a, b) => {
    if (typeof a == 'number' && typeof b == 'number') {
        return a + b
    } else {
        return 0
    }
}
  1. 返回对象时需要用小括号包起来,因为大括号被占用解释为代码块了
var getHash = arr => {
    // ...
    return ({
        name: 'Jack',
        age: 33
    })
}
  1. 直接作为事件handler
document.addEventListener('click', ev => {
    console.log(ev)
})
  1. 作为数组排序回调
var arr = [1, 9 , 2, 4, 3, 8].sort((a, b) => {
    if (a - b > 0 ) {
        return 1
    } else {
        return -1
    }
})
arr // [1, 2, 3, 4, 8, 9]

二、注意点

  1. typeof运算符和普通的function一样
var func = a => a
console.log(typeof func); // "function"
  1. instanceof也返回true,表明也是Function的实例
console.log(func instanceof Function); // true
  1. this固定,不再善变
obj = {
    data: ['John Backus', 'John Hopcroft'],
    init: function() {
        document.onclick = ev => {
            alert(this.data) // ['John Backus', 'John Hopcroft']
        }
        // 非箭头函数
        // document.onclick = function(ev) {
        //     alert(this.data) // undefined
        // }
    }
}
obj.init()

这个很有用,再不用写me,self,_this了,或者bind。

  1. 箭头函数不能用new
var Person = (name, age) => {
    this.name = name
    this.age = age
}
var p = new Func('John', 33) // error
  1. 不能使用argument
var func = () => {
    console.log(arguments)
}
func(55) //

对于5,在Firefox36里测试是可以输出55的,貌似并没有这个限制

参考:https://www.cnblogs.com/snandy/p/4403111.html
———https://www.cnblogs.com/zhenwen/p/5724236.html
———https://www.jianshu.com/p/456f66db3159

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值