js的map、filter的用法

2020-04-01 10:14:02

filter是满足条件的留下,是对原数组的过滤;

map则是对原数组的加工,映射成一一映射的新数组

简单例子:

  let arr = [1, 2, 3, 4]

  let newArr = arr.map(function(item) {  // 使用map方法

    return item * 2;

  });

  console.log(newArr); // [2, 4, 6, 8]

  let arr = [1, 2, 3, 4];

  let newArr = arr.filter(function(item) {  // 使用filter方法

    if (item % 2 !== 0) {

      return item;

     }

   });

  console.log(newArr); // [1, 3];

  let newArr = arr.filter( item => item % 2 !== 0)  // 箭头函数不加{}自动return,加{}必须用return

  console.log(newArr); // [1, 3];


  • 2020-02-19 23:12:44

    Laravel 从 $request 到 $response 的过程解析二(必读)

    laravel 的请求会组装成 $request 对象,然后会依次经过中间件(前置部分),最终到达 url 指向的控制器方法,然后把返回数据组装为 $response 对象返回,再依次经过中间件 (后置部分),最终返回。

  • 2020-02-19 23:15:24

    PHP 闭包(Closure)

    闭包(Closure)又叫做匿名函数,也就是没有定义名字的函数。比如下面的例子:

  • 2020-02-19 23:26:58

    php array_pop 删除数组最后一个元素实例

    php array_pop函数将数组最后一个单元弹出(出栈),即删除数组的最后一个元素。本文章通过php实例向大家讲解array_pop函数的使用方法。