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

Time:03-07

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

[
    {
        "title": "The Great Peace",
        "copyversion": 1
    },
    {
        "title": "History of World War II",
        "copyversion": 1
    },
    {
        "title": "Crime and Punishment",
        "copyversion": 2
    },
    {
        "title": "War and Peace",
        "copyversion": 2
    }
]

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.

[
    {
        "title": "The Great Peace",
        "copyversion": 1
    },
    {
        "title": "History of World War II",
        "copyversion": 1
    }
]

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"

[
    {
        "title": "The Great Peace",
        "copyversion": 1
    },
    {
        "title": "History of World War II",
        "copyversion": 1
    },
    {
        "title": "Crime and Punishment",
        "copyversion": 2
    },
    {
        "title": "War and Peace",
        "copyversion": 2
    }
]

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.

CodePudding user response:

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.

const a = [
    {
        "title": "The Great Peace",
        "copyversion": 1
    },
    {
        "title": "History of World War II",
        "copyversion": 1
    },
    {
        "title": "Crime and Punishment",
        "copyversion": 2
    },
    {
        "title": "War and Peace",
        "copyversion": 2
    }
];

const copyFinder = (word, arr) => {
  const versions = arr.reduce((collector, value) => {
    if(value.title.indexOf(word) > -1 && collector.indexOf(value.copyversion) === -1) {
      collector.push(value.copyversion);
    }
    return collector;
  }, []);
  if(versions.length === 0) {
    return [];
  }
  return arr.filter(x => versions.indexOf(x.copyversion) > -1);
}

console.log(copyFinder('History', a));

CodePudding user response:

That is my simple and readable solution. Hope it will useful for you

const books = [
    {
        "title": "The Great Peace",
        "copyversion": 1
    },
    {
        "title": "History of World War II",
        "copyversion": 1
    },
    {
        "title": "Crime and Punishment",
        "copyversion": 2
    },
    {
        "title": "War and Peace",
        "copyversion": 2
    }
];

const findBooks = (titlePart) => {
    const regexp = new RegExp(`${titlePart}`, 'i');
    const resultSet = new Set();
    const copyVersionsSet = new Set();

    // Find all books which has titlePart in title
    for (const book of books) {
        if (regexp.test(book.title)) {
            resultSet.add(book);
            copyVersionsSet.add(book.copyversion);
        }
    }
  
    // Find all books which has same copyversion as found books
    for (const book of books) {
        if (copyVersionsSet.has(book.copyversion)) {
            resultSet.add(book);
        }
    }

    return [...resultSet];
}

console.log(findBooks('hIsToRy'));

  • Related