我可以将对象数组传递给 filterBy 吗?
Can I pass an array of object to filterBy?
我的ember版本是1.13
,请问下面这行代码是否适用于我上面的ember应用版本?
console.log(this.get('arrayOfObjects').filterBy('zoneName', ['zoneNameOne', 'zoneNameTwo']));
?
selectedZoneOrCityName
的样本数据为
selectedZoneOrCityName = ['zoneNameOne', 'zoneNameTwo'],
我想像这样使用它
if (selectedZoneOrCityName) {
return this.get('arrayOfObjects').filterBy('zoneName', selectedZoneOrCityName).mapBy('cityName');
} else {
console.log('reads nothing');
return [];
}
您可以像下面的代码片段一样使用简单的 filter
。
var arrayOfObjects = [
{
id: 1,
name: 'one',
zoneName: 'zoneNameOne'
},
{
id: 2,
name: 'two',
zoneName: 'one zoneName'
},
{
id: 3,
name: 'three',
zoneName: 'zoneNameOne'
},
{
id: 4,
name: 'four',
zoneName: 'zoneNameTwo'
}
];
var selectedZoneOrCityName = ['zoneNameOne', 'zoneNameTwo'];
arrayOfObjects = arrayOfObjects.filter((item) => {
return selectedZoneOrCityName.includes(item.zoneName);
});
console.log('final filtered array : ', arrayOfObjects);
如果您使用 filterBy
,那么您必须 chain
对每个数组值进行过滤。
我的ember版本是1.13
,请问下面这行代码是否适用于我上面的ember应用版本?
console.log(this.get('arrayOfObjects').filterBy('zoneName', ['zoneNameOne', 'zoneNameTwo']));
?
selectedZoneOrCityName
的样本数据为
selectedZoneOrCityName = ['zoneNameOne', 'zoneNameTwo'],
我想像这样使用它
if (selectedZoneOrCityName) {
return this.get('arrayOfObjects').filterBy('zoneName', selectedZoneOrCityName).mapBy('cityName');
} else {
console.log('reads nothing');
return [];
}
您可以像下面的代码片段一样使用简单的 filter
。
var arrayOfObjects = [
{
id: 1,
name: 'one',
zoneName: 'zoneNameOne'
},
{
id: 2,
name: 'two',
zoneName: 'one zoneName'
},
{
id: 3,
name: 'three',
zoneName: 'zoneNameOne'
},
{
id: 4,
name: 'four',
zoneName: 'zoneNameTwo'
}
];
var selectedZoneOrCityName = ['zoneNameOne', 'zoneNameTwo'];
arrayOfObjects = arrayOfObjects.filter((item) => {
return selectedZoneOrCityName.includes(item.zoneName);
});
console.log('final filtered array : ', arrayOfObjects);
如果您使用 filterBy
,那么您必须 chain
对每个数组值进行过滤。