Emberjs filter() 与 filterProperty() [英] Emberjs filter() versus filterProperty()
问题描述
看起来像 filter() 和 filterProperty() 非常相似,都是返回过滤数组的 Enumerable 函数.
在什么情况下我应该使用一种或另一种?
更新: filterProperty()
已替换为 filterBy()
.用法相同,见下方评论.
filterBy()
是 filter()
的快捷方式,可让您根据枚举元素的指定属性快速过滤枚举.如果您需要做一些更复杂或不寻常的事情,而您不能使用 filterBy()
,请使用 filter()
.
例如,假设您有一个这样的对象数组:
<预><代码>[{名字:'克里斯',姓氏:'塞尔登'},{firstName: 'Luke', lastName: 'Melia'},{名字:'以前的亚历克斯',姓氏:'Matchneer'}]并且您想要一个计算属性,该属性使用过滤器数组来仅包含具有 firstName == 'Luke'
的人:
使用filter()
:
filterComputed: function() {返回 this.get('content').filter(function(item, index, enumerable){返回 item.firstName == 'Luke';});}.property('content.@each')
使用filterBy()
:
filterByComputed: function() {返回 this.get('content').filterBy('firstName', 'Luke');}.property('content.@each')
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?
Update: filterProperty()
has been replaced by filterBy()
. The usage is the same, see comment below.
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'}
]
And you wanted to have a computed property that uses filter the array to only include people with the firstName == 'Luke'
:
using filter()
:
filterComputed: function() {
return this.get('content').filter(function(item, index, enumerable){
return item.firstName == 'Luke';
});
}.property('content.@each')
using filterBy()
:
filterByComputed: function() {
return this.get('content').filterBy('firstName', 'Luke');
}.property('content.@each')
这篇关于Emberjs filter() 与 filterProperty()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!