Skip to content
Advertisement

filter an array of objects in javascript, with siblings that match on a common key value

Suppose I have a javascript array of objects that looks like this:

JavaScript

Now, suppose that I have a search string, like “War” or “and”. I want to get an array of objects where “title” contains the search string (case insensitive), but I want to ALSO include any sibling values with matching “copyversion” values.

For example:

Search string of “Great” should yield the below result, because even though “History of World War II” does not have “Great” in it, it matches the copyversion of something that does.

JavaScript

Another example:

Search string of “Peace” would yield the original array. “History of World War II” is included because it has the same copyversion value as “The Great Peace”, and “Crime and Punishment” is included because it has the same copyversion as “War and Peace”

JavaScript

If no matches are found, then an empty array would result.

I’m looking for a reasonable fast way to do this. I’m fine with pure javascript or a library like lodash.

Advertisement

Answer

Solution contains two parts:

  1. find all matching objects and collect their copyversion. Do not store duplicates.
  2. return all objects with the corresponding copyversion.

The first part may be optimized – we don’t have to remove duplicates.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement