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

96
Views
Filter array of objects whose specific properties contains a value

I'm trying to find the cleanest way to implement a filter to an array of objects depending on a string keyword. I need to return those objects whose only specific properties contains the string value.

Let's say I have the following array of objects:

 const products = [
      {
        name: 'car',
        price: 100,
        image: 'someurl'
        email: 'car@car.car'
        available: true,
      }, 
      {
        name: 'phone',
        price: 200,
        image: 'someurl'
        email: 'phone@phone.phone'
        available: false,
      }, 
      {
        name: 'bottle',
        price: 300,
        image: 'someurl'
        email: 'bottle@bottle.bottle'
        available: true,
      }, 
    ];

As mentioned here: Filter array of objects whose any properties contains a value

One of the cleanest way to match any value in an array of objects whose properties have different types would be:

function filterByValue(array, string) {
    return array.filter(o =>
        Object.keys(o).some(k => String(o[k]).toLowerCase().includes(string.toLowerCase())));
}

This filterByValue function will return those objects whose any properties match the string value.

However, I'd like to add some conditions, so it only iterates and look for some match at "name", "price" and "email" properties, not looking at "image" and "available" properties.

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

0

Make an array of the properties to look at, and look at them instead of using Object.keys.

const propsToCheck = ['name', 'price', 'email'];
function filterByValue(array, string) {
    return array.filter(o =>
        propsToCheck.some(k => String(o[k]).toLowerCase().includes(string.toLowerCase())
        )
    );
}
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!