Lodash过滤器有限制

时间:2016-08-12 17:39:32

标签: javascript lodash

有没有办法过滤符合条件的数组中的前n个元素?

我知道您可以filter然后拨打take但是没有通过整个列表而不是在达到限制后退出?

_(books).filter(function(book) {return book.pages > 10}).take(5).value();

6 个答案:

答案 0 :(得分:2)

如果传递给_.forEach()函数的迭代器函数返回false,则会立即停止迭代。

以下filter函数说明了_.forEach()如何用于解决您的用例。

function filter(books, maxCount) {
  var results = [];
  _.forEach(books, function(book) {
    if (results.length === maxCount) {
      return false;
    }
    if (book.pages > 10) {
      results.push(book);
    }
  });
  return results;
}


var N = 5;
var books = new Array(20);

// Give each book a page count equal to its books index.
_.forEach(books, function(v, i) {books[i] = {pages: i}});

console.log(filter(books, N));
<script src="https://cdn.jsdelivr.net/lodash/4.15.0/lodash.min.js"></script>

答案 1 :(得分:0)

您的解决方案几乎适用于所有情况,除非您必须处理大量数据。无法想到更有效的lodash解决方案,但这就足够了:

select m.mname, s.lang, s.sdesc, l.lang, l.ldesc
from main m left join
     short_desc s
     on s.id = m.sid left join
     long_desc l
     on l.id = m.lid and (s.lang = l.lang or s.lang is null);

答案 2 :(得分:0)

另一种方式

var limit = 5,
    result = [];
_.forEach(books, function(book) {
    if (limit--) {
        return false;
    } else {
        if (book.pages > 10) {
            result.push(book);
        }
    }
});

答案 3 :(得分:0)

据我所知,只有一个lodash函数允许你提前终止,_.transform

result = _(books).transform(function(result, book) {
	book.pages > 10 && result.push(book);
	return result.length < 5;
}).value();

document.write(JSON.stringify(result));
<script src="https://cdn.rawgit.com/lodash/lodash/4.15.0/dist/lodash.min.js"></script>
<script>
  var books = [
    { title: 'a', pages: 8 },
    { title: 'b', pages: 10 },
    { title: 'c', pages: 12 },
    { title: 'd', pages: 14 },
    { title: 'e', pages: 16 },
    { title: 'f', pages: 5 },
    { title: 'g', pages: 18 },
    { title: 'h', pages: 20 }
  ]
</script>

当然,您也可以使用mixin创建自己的API:

// define mixin once somewhere:
_.mixin({'findWhile': function(collection, predicate, num) {
  return _.transform(collection, function(result, value) {
    predicate(value) && result.push(value);
    return result.length < num;
  });
}});

// then to use it:
var result = _(books).findWhile(function(book) { return book.pages > 10 }, 5).value();

document.write(JSON.stringify(result));
<script src="https://cdn.rawgit.com/lodash/lodash/4.15.0/dist/lodash.min.js"></script>
<script>
  var books = [
    { title: 'a', pages: 8 },
    { title: 'b', pages: 10 },
    { title: 'c', pages: 12 },
    { title: 'd', pages: 14 },
    { title: 'e', pages: 16 },
    { title: 'f', pages: 5 },
    { title: 'g', pages: 18 },
    { title: 'h', pages: 20 }
  ]
</script>

答案 4 :(得分:0)

    book.pages.push(page);
    if(book.pages.length > WHATEVER){
        book.pages = _.drop(book.pages, book.pages.length - WHATEVER );
    }

答案 5 :(得分:-1)

您只需设置数组的长度

即可
var numbers = [1, 2, 3, 4, 5];

if (numbers.length > 3) {
    numbers.length = 3;
}

console.log(numbers); // [1, 2, 3]
console.log(numbers.length); // 3

请参阅MDN webdocs Array.length