Skip to content

JavaScript filter array of objects using includes

  • by

To filter an array of objects based on a specific property using the includes method in JavaScript, you can utilize the filter method along with includes.

Here’s the syntax for filtering an array of objects using the includes method in JavaScript:

const arr = originalArray.filter(item => item.property.includes(searchTerm));
  • originalArray is the array of objects you want to filter.
  • item is the individual object within the array that will be evaluated in the filtering process.
  • property is the specific property of each object that you want to check for inclusion.
  • searchTerm is the value you want to search for within the specified property.

By using the filter method on the originalArray, you can create a new array filteredArray that only contains the objects whose property value includes the searchTerm.

By applying this approach, you can selectively extract objects from the array that contain a particular value within the desired property.

JavaScript filter array of objects with includes example

Simple example code.

We define a searchTerm variable that holds the string we want to search for. In this case, it is set to 'an'.

const items = [
  { id: 1, name: 'Apple' },
  { id: 2, name: 'Banana' },
  { id: 3, name: 'Orange' },
  { id: 4, name: 'Grape' }
];

const searchTerm = 'an';

const filteredItems = items.filter(item => item.name.includes(searchTerm));

console.log(filteredItems);

Output:

JavaScript filter array of objects using includes

Filter array of objects whose properties contain a value

If you want to filter an array of objects based on multiple properties and check if any of those properties contain a specific value, you can use the filter method in combination with the some method. The some method checks if at least one element in an array satisfies a condition.

const products = [
  { id: 1, name: 'Apple', category: 'Fruit', color: 'Red' },
  { id: 2, name: 'Banana', category: 'Fruit', color: 'Yellow' },
  { id: 3, name: 'Orange', category: 'Fruit', color: 'Orange' },
  { id: 4, name: 'Carrot', category: 'Vegetable', color: 'Orange' },
  { id: 5, name: 'Broccoli', category: 'Vegetable', color: 'Green' }
];

const searchValue = 'Orange';

const filteredProducts = products.filter(product => {
  return Object.values(product).some(value => {
    if (typeof value === 'string' && value.includes(searchValue)) {
      return true;
    }
    return false;
  });
});

console.log(filteredProducts);

To filter an array of objects based on whether their properties contain a specific value, you can use the Array.prototype.filter() method along with the Object.values() method.

const data = [
  { id: 1, name: 'John', age: 25 },
  { id: 2, name: 'Jane', age: 30 },
  { id: 3, name: 'Alice', age: 35 },
  { id: 4, name: 'Bob', age: 25 }
];

const filteredData = data.filter(obj =>
  Object.values(obj).some(value =>
    typeof value === 'string' && value.includes('Jo')
  )
);

console.log(filteredData);

Comment if you have any doubts or suggestions on this JS Array of Object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *