ES6 – Is there a way to filter deeply nested object by a search term

ES6 – Is there a way to filter deeply nested object by a search term

When working with deeply nested objects in JavaScript, it can sometimes be challenging to filter and extract specific data based on a search term. However, with the introduction of ES6, there are multiple approaches you can take to achieve this. In this article, we will explore two common solutions.

Solution 1: Using recursion

One way to filter a deeply nested object by a search term is by using recursion. Recursion is a technique where a function calls itself until a specific condition is met. Here’s an example of how you can implement this:


const filterBySearchTerm = (obj, searchTerm) => {
  let result = {};

  for (let key in obj) {
    if (typeof obj[key] === 'object') {
      result[key] = filterBySearchTerm(obj[key], searchTerm);
    } else if (typeof obj[key] === 'string' && obj[key].includes(searchTerm)) {
      result[key] = obj[key];
    }
  }

  return result;
};

// Usage
const nestedObj = {
  name: 'John',
  age: 25,
  address: {
    street: '123 Main St',
    city: 'New York',
    country: 'USA',
    contact: {
      email: 'john@example.com',
      phone: '123-456-7890'
    }
  }
};

const filteredObj = filterBySearchTerm(nestedObj, 'John');
console.log(filteredObj);

The above code defines a function filterBySearchTerm that takes an object and a search term as parameters. It iterates over each key in the object and checks if the value is an object itself. If it is, the function calls itself recursively to filter the nested object. If the value is a string and contains the search term, it adds it to the result object. Finally, it returns the filtered object.

Solution 2: Using JSON.stringify and JSON.parse

Another approach is to convert the deeply nested object into a JSON string using JSON.stringify, filter the string using regular expressions, and then parse it back into an object using JSON.parse. Here’s an example:


const filterBySearchTerm = (obj, searchTerm) => {
  const jsonString = JSON.stringify(obj);
  const filteredString = jsonString.replace(new RegExp(searchTerm, 'g'), '');
  const filteredObj = JSON.parse(filteredString);

  return filteredObj;
};

// Usage
const nestedObj = {
  name: 'John',
  age: 25,
  address: {
    street: '123 Main St',
    city: 'New York',
    country: 'USA',
    contact: {
      email: 'john@example.com',
      phone: '123-456-7890'
    }
  }
};

const filteredObj = filterBySearchTerm(nestedObj, 'John');
console.log(filteredObj);

In this solution, the filterBySearchTerm function converts the object into a JSON string using JSON.stringify. It then uses the replace method with a regular expression to filter out the search term from the string. Finally, it parses the filtered string back into an object using JSON.parse and returns it.

Both of these solutions provide a way to filter deeply nested objects by a search term in ES6. Choose the one that best suits your needs and the structure of your data.

That’s it for this article! We hope you found it helpful in solving your problem of filtering deeply nested objects. If you have any questions or alternative solutions, feel free to leave a comment below.


Posted

in

by

Tags:

Comments

Leave a Reply

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