• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

142
Views
filtering array of objects in react not working

I have an array of objects like below:

0: {Id: 1, name: 'xyz', pqID: 10, pqType: null}
1: {Id: 2, name: 'abc', pqID: 15, pqType: null}
2: {Id: 3, name: 'wer', pqID: 16, pqType: null}
3: {Id: 4, name: 'uyt', pqID: 18, pqType: null}
4: {Id: 5, name: 'qwe', pqID: 22, pqType: null}
5: {Id: 6, name: 'ert', pqID: 25, pqType: null}

I want objects of pqID and 10 and 15. Below is what I am trying which is giving empty array:

const newUsers = arr.filter(
    (user) => user.pqID == 10 && user.pqID == 15
);

console.log(newUsers);
almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Note the || operator

var arr = 
[{Id: 1, name: 'xyz', pqID: 10, pqType: null},
{Id: 2, name: 'abc', pqID: 15, pqType: null},
{Id: 3, name: 'wer', pqID: 16, pqType: null},
{Id: 4, name: 'uyt', pqID: 18, pqType: null},
{Id: 5, name: 'qwe', pqID: 22, pqType: null},
{Id: 6, name: 'ert', pqID: 25, pqType: null}]

 const newUsers = arr.filter(
      (user) =>
       user.pqID == 10 || user.pqID == 15 // note ||
     );

console.log(newUsers)

almost 3 years ago · Juan Pablo Isaza Report

0

You could try that, with full array function syntax:

const newUsers = arr.filter(
    (user) => {return [10, 15].includes(user.pqID)}
);

Or the minified version, without parentheses and curly brackets:

const newUsers = arr.filter(user => [10, 15].includes(user.pqID));
almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error