Array filter creates a new array with all elements that pass the test implemented by the provided function. In this lesson we discuss how only a truthy or falsey value is required as the return value to the function, which in turns allows us to be creative in how we perform the filter. We end the lesson by looking at an example showing how chaining multiple array methods together can lead to very nice, declarative code.

  1. const lessons = [
  2. {
  3. title: 'Javascript Arrays in Depth - join',
  4. views: 960,
  5. tags: ['array', 'join']
  6. },
  7. {
  8. title: 'Javascript Arrays in Depth - concat',
  9. views: 1050,
  10. tags: ['array', 'concat']
  11. },
  12. {
  13. title: 'Javascript Arrays in Depth - slice',
  14. views: 2503,
  15. tags: ['array', 'slice']
  16. },
  17. {
  18. title: 'Javascript Functions in Depth - bind',
  19. views: 2500,
  20. tags: ['functions', 'bind']
  21. }
  22. ];
  23.  
  24. const minViews = 1000;
  25. const searchTerm = 'array';
  26.  
  27. const filtered = lessons
  28. .filter(x => x.tags.indexOf(searchTerm) > -1)
  29. .filter(x => x.views > minViews)
  30. .sort((a, b) => b.views - a.views)
  31. .map(x => ` <li>${x.title}</li>`)
  32. .join('\n');
  33.  
  34. console.log(`<ul>
  35. ${filtered}
  36. </ul>`);

[Javascript] Array methods in depth - filter的更多相关文章

  1. [Javascript ] Array methods in depth - sort

    Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...

  2. [Javascript] Array methods in depth - slice

    Array slice creates a shallow copy of an array. In this lesson we cover, in detail, exactly what a ' ...

  3. [Javascript] Array methods in depth - indexOf

    indexOf is used to search for a value or reference inside of an array. In this lesson we first look ...

  4. [Javascript] JavaScript Array Methods in Depth - push

    Array push is used to add elements to the end of an Array. In this lesson we'll see how the push met ...

  5. [Javascript] Array methods in depth - some

    some returns a boolean value after passing each item in the source array through the test function t ...

  6. JavaScript Array methods performance compare

    JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...

  7. javascript Array Methods(学习笔记)

    ECMAScript 5 定义了9个新的数组方法,分别为: 1.forEach();  2.map();  3.filter();  4.every();  5.some();  6.reduce() ...

  8. JavaScript Array filter() 方法

    JavaScript Array filter() 方法 var ages = [32, 33, 16, 40]; function checkAdult(age) { return age > ...

  9. JavaScript Array -->map()、filter()、reduce()、forEach()函数的使用

    题目: 1.得到 3000 到 3500 之内工资的人. 2.增加一个年龄的字段,并且计算其年龄. 3.打印出每个人的所在城市 4.计算所有人的工资的总和. 测试数据: function getDat ...

随机推荐

  1. Swift - 13 - 字符串和Character

    //: Playground - noun: a place where people can play import UIKit var str = "hi" // 字符串拼接 ...

  2. JavaScript_object基础

    之前写Java时老是有点蒙,大部分都是用jQuery,但原理还不是很清楚,最近一段时间在系统的学习JavaScript,有什么问题或错误请指出,多谢..................... Obje ...

  3. linux 进程数

    一.linux系统支持的最大进程数 限制1:既然系统使用pid_t表示进程号,那么最大进程数不能超过pid_t类型的最大值吧 限制2:使用命令ulimit -u查看系统中限制的最大进程数,我的机器上是 ...

  4. underscorejs-sortBy学习

    2.17 sortBy 2.17.1 语法 _.sortBy(list, iteratee, [context]) 2.17.2 说明 返回一个排序后的list拷贝副本. list为集合,如数组.对象 ...

  5. js移动设备手机跳转地址代码

    if(/AppleWebKit.*mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alc ...

  6. 关于Python文档读取UTF-8编码文件问题

    近来接到一个小项目,读取目标文件中每一行url,并逐个请求url,拿到想要的数据. #-*- coding:utf-8 -*- class IpUrlManager(object): def __in ...

  7. oracle导入

    1 建立all.sql脚本,此脚本内容指定了insert脚本的路径all.sql脚本内容如下: @D:\workspace\workspace-二期开始使用的文件\Test\数据库升级工具\导入基础数 ...

  8. 转:So Easy!让开发人员更轻松的工具和资源

    Cascade Framework 很独特的 CSS 框架,进行了模块化划分,分类排版.表格.颜色.图标和打印样式等等. Mueller Grid System 一个模块化的网格系统,用于响应式或者固 ...

  9. Color the ball

    hdu1556:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意:中文题. 题解:这一题当然可以直接用线段树来打,但是最近在学树状数组,所以用树状数组 ...

  10. Jzzhu and Cities

    CF #257 div2D:http://codeforces.com/contest/450/problem/D 题意:给你n个城市,m条无向有权边.另外还有k条边,每条边从起到到i.求可以删除这k ...