 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Filter nested object by keys using JavaScript
Suppose, we have an array of objects like this −
const arr = [{ 'title': 'Hey',    'foo': 2,    'bar': 3 }, {    'title': 'Sup',    'foo': 3,    'bar': 4 }, {    'title': 'Remove',    'foo': 3,    'bar': 4 }]; We are required to write a JavaScript function that takes in one such array as the first input and an array of string literals as the second input.
Our function should then prepare a new array that contains all those objects whose title property is partially or fully included in the second input array of literals.
Example
The code for this will be −
const arr = [{ 'title': 'Hey',    'foo': 2,    'bar': 3 }, {    'title': 'Sup',    'foo': 3,    'bar': 4 }, {    'title': 'Remove',    'foo': 3,    'bar': 4 }]; const filterTitles = ['He', 'Su']; const filterByTitle = (arr = [], titles = []) => {    let res = [];    res = arr.filter(obj => {       const { title } = obj;       return !!titles.find(el => title.includes(el));    });    return res; }; console.log(filterByTitle(arr, filterTitles));  Output
And the output in the console will be −
[ { title: 'Hey', foo: 2, bar: 3 }, { title: 'Sup', foo: 3, bar: 4 } ]Advertisements
 