本文介绍了Emberjs filter()与filterProperty()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像和非常相似,都是Enumerable函数返回过滤的数组。

It seems like filter() and filterProperty() are quite similar, both are Enumerable functions returning a filtered array.

在什么情况下我应该使用一个或另一个?

In what circumstances should I use one or the other?

推荐答案

更新: filterProperty()已被替换为 filterBy()。使用情况是一样的,见下面的评论。

Update: filterProperty() has been replaced by filterBy(). The usage is the same, see comment below.

filterBy()是 filter(),可以让您根据枚举的元素的指定属性快速过滤枚举。使用 filter()如果你需要做一些更复杂或不常用的东西,你不能使用 filterBy()

filterBy() is a shortcut for filter() that lets you quickly filter an enumerable based on a specified property of the elements of the enumerable. Use filter() if you need to do something more complicated or out-of-the-ordinary where you can't use filterBy().

例如,假设你有一个这样的对象数组:

For example, assuming you had an array of objects like this:

[
  {firstName: 'Kris', lastName: 'Selden'},
  {firstName: 'Luke', lastName: 'Melia'},
  {firstName: 'Formerly Alex', lastName: 'Matchneer'}
]

有一个计算属性,使用过滤器数组只包括具有 firstName =='Luke'的人:

And you wanted to have a computed property that uses filter the array to only include people with the firstName == 'Luke':

使用 filter()

using filter():

filterComputed: function() {
  return this.get('content').filter(function(item, index, enumerable){
    return item.firstName == 'Luke';
  });
}.property('content.@each')

使用 filterBy()

using filterBy():

filterByComputed: function() {
  return this.get('content').filterBy('firstName', 'Luke');
}.property('content.@each')

这篇关于Emberjs filter()与filterProperty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:57