Home > Software engineering >  How i can filter array by another array if 1 of elements equal?
How i can filter array by another array if 1 of elements equal?

Time:03-28

My array for filter:

types: [
  { type: 0, name: 'Item_1' },
  { type: 1, name: 'Item_2' },
  { type: 2, name: 'Item_3' },
  { type: 3, name: 'Item_4' },
  { type: 4, name: 'Item_5' },
  { type: 5, name: 'Item_6' },
  { type: 6, name: 'Item_7' },
]

filterBy:

sort: [1, 2, 3];

How i can do smth like this but for multiple values?

    filterType(item) {
        return item.type.toString() == this.sort;
    }

UPDATE: Need for this function:

filtered() {
        const conditions = [];


        if (this.sort) {
            conditions.push(this.filterType);
        }

        if (conditions.length > 0) {
            return this.types.filter((dessert) => {
                return conditions.every((condition) => {
                    return condition(dessert);
                })
            })
        }

        return this.types;
    }

I already treid to make for and foreach loop but it doesn`t work

CodePudding user response:

Try this way:

const types = [
  { type: 0, name: 'Item_1' },
  { type: 1, name: 'Item_2' },
  { type: 2, name: 'Item_3' },
  { type: 3, name: 'Item_4' },
  { type: 4, name: 'Item_5' },
  { type: 5, name: 'Item_6' },
  { type: 6, name: 'Item_7' },
];

const needTypes = new Set([1, 2, 3]);

const filterTypes = (arr) => arr.filter(item => needTypes.has(item.type));

console.log(filterTypes(types));

I used new Set() because if needTypes will be a very large array then it will take much time to search in array element every time

CodePudding user response:

Well, you can make an object out of your sort array

const something={
  types: [
    { type: 0, name: 'Item_1' },
    { type: 1, name: 'Item_2' },
    { type: 2, name: 'Item_3' },
    { type: 3, name: 'Item_4' },
    { type: 4, name: 'Item_5' },
    { type: 5, name: 'Item_6' },
    { type: 6, name: 'Item_7' },
  ],
  sort: [1, 2, 3],
  filterType: function filterType() {
    if(!this.sortObj){
      this.sortObj={} //to make it faster than indexOf in the long run
      this.sort.forEach(item=>this.sortObj[item]=true)
    }
    return this.types.filter(item=>this.sortObj[item.type]);
  }
}

//I just have the filterType function use this for everything because of the limited layout given
//You can change the types array and the sort array, then run the filterType function to get different values
//example usage below
console.log(something.filterType())

CodePudding user response:

It looks like you're using a class. So pass in the types array, and the sort array, and then call the class's filter method to return those filtered objects.

Something like this.

class Desserts {

  constructor(types, sort) {
    this.types = types;
    this.sort = sort;
  }

  add(obj) {
    this.types.push(obj);
  }
  
  updateSort(n) {
    this.sort.push(n);
  }

  filter() {
    return this.types.filter(dessert => {
      return this.sort.includes(dessert.type);
    });
  }

}

const types = [{type:0,name:'Item_1'},{type:1,name:'Item_2'},{type:2,name:'Item_3'},{type:3,name:'Item_4'},{type:4,name:'Item_5'},{type:5,name:'Item_6'},{type:6,name:'Item_7'}];

const sort = [1, 2, 3];

const desserts = new Desserts(types, sort);

console.log(desserts.filter());

desserts.add({ type:7, name:'Item_8' });
desserts.updateSort(7);

console.log(desserts.filter());

  • Related