Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How i can filter array by another array if 1 of elements equal?

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!