码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Array methods in depth - filter

时间:2015-12-19 06:34:49      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

const lessons = [
    {
        title: ‘Javascript Arrays in Depth - join‘,
        views: 960,
        tags: [‘array‘, ‘join‘]
    },
    {
        title: ‘Javascript Arrays in Depth - concat‘,
        views: 1050,
        tags: [‘array‘, ‘concat‘]
    },
    {
        title: ‘Javascript Arrays in Depth - slice‘,
        views: 2503,
        tags: [‘array‘, ‘slice‘]
    },
    {
        title: ‘Javascript Functions in Depth - bind‘,
        views: 2500,
        tags: [‘functions‘, ‘bind‘]
    }
];

const minViews = 1000;
const searchTerm = ‘array‘;

const filtered = lessons
    .filter(x => x.tags.indexOf(searchTerm) > -1)
    .filter(x => x.views > minViews)
    .sort((a, b) => b.views - a.views)
    .map(x => `  <li>${x.title}</li>`)
    .join(‘\n‘);

console.log(`<ul>
${filtered}
</ul>`);

 

[Javascript] Array methods in depth - filter

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5058574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!