首页 > 编程语言 > 详细

[Javascript] Array methods in depth - filter

时间:2015-12-19 06:33:48      阅读:305      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!