
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Filter array of objects whose properties contains a value in JavaScript
Suppose, we have an array of objects like this −
const arr = [{ name: 'Paul', country: 'Canada', }, { name: 'Lea', country: 'Italy', }, { name: 'John', country: 'Italy', }, ];
We are required to devise a way to filter an array of objects depending on a string keyword. The search has to be made in any properties of the object.
For instance −
When we type "lea", we want to go through all the objects and all their properties to return the objects that contain "lea". When we type "italy", we want to go through all the objects and all their properties to return the objects that contain italy.
Example
The code for this will be −
const arr = [{ name: 'Paul', country: 'Canada', }, { name: 'Lea', country: 'Italy', }, { name: 'John', country: 'Italy', }, ]; const filterByValue = (arr = [], query = '') => { const reg = new RegExp(query,'i'); return arr.filter((item)=>{ let flag = false; for(prop in item){ if(reg.test(item[prop])){ flag = true; } }; return flag; }); }; console.log(filterByValue(arr, 'ita'));
Output
And the output in the console will be −
[ { name: 'Lea', country: 'Italy' }, { name: 'John', country: 'Italy' } ]
- Related Articles
- Filter JavaScript array of objects with another array
- Filter array of objects by a specific property in JavaScript?
- Filter an array containing objects based on another array containing objects in JavaScript
- Sort Array of objects by two properties in JavaScript
- Sort an array of objects by multiple properties in JavaScript
- Map multiple properties in array of objects to the same array JavaScript
- How to access properties of an array of objects in JavaScript?
- Creating an array using a string which contains the key and the value of the properties - JavaScript
- Sort array of objects by string property value in JavaScript
- Find specific key value in array of objects using JavaScript
- Retrieve property value selectively from array of objects in JavaScript
- Sort array of objects by string property value - JavaScript
- Filter array with filter() and includes() in JavaScript
- Manipulating objects in array of objects in JavaScript
- How to calculate the average in JavaScript of the given properties in the array of objects

Advertisements