我可以将对象数组传递给filterBy吗?

时间:2018-10-18 06:12:06

标签: ember.js ember-data

我的余烬版本为1.13,我想问一下下面的代码行是否适用于我的余烬应用程序的版本?

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 [];
}

1 个答案:

答案 0 :(得分:1)

您可以使用简单的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为每个数组值进行filterBy。