Mikelemuel
Mikelemuel

Reputation: 378

Can I pass an array of object to filterBy?

My ember version is 1.13 and I would like to ask if the line of code below is applicable to the version of my ember app above?

console.log(this.get('arrayOfObjects').filterBy('zoneName', ['zoneNameOne', 'zoneNameTwo'])); ?

Sample data of selectedZoneOrCityName is

selectedZoneOrCityName = ['zoneNameOne', 'zoneNameTwo'],

I want to use it something like these

if (selectedZoneOrCityName) {
  return this.get('arrayOfObjects').filterBy('zoneName', selectedZoneOrCityName).mapBy('cityName');
} else {
  console.log('reads nothing');
  return [];
}

Upvotes: 0

Views: 295

Answers (1)

Shiv Kumar Baghel
Shiv Kumar Baghel

Reputation: 2480

you can use simple filter like below code snippet.

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);

if you are usign filterBy then you have to chain filterBy for each array value.

Upvotes: 1

Related Questions