Home > database >  Filter specific key(s) in array of objects that contain search term in any specified key value
Filter specific key(s) in array of objects that contain search term in any specified key value

Time:08-08

What woule be the best/fastest way to filter array of objects by searcing for specific term in specific key value pair(s)?

Let's say we have an array of objects, array of desired keys and search term string.

The array of objects:

const posts = [
  {
     postId: 1,
     title: "Hello my friends", 
     text: "To all my friends",
     description: "Random text"
  },
  {
     postId: 2,
     title: "To all my friends", 
     text: "Hello my friends",
     description: "Hello text"
  },
  {
     postId: 3,
     title: "My friends", 
     text: "To all my friends",
     description: "Random Hello",
  },
  {
     postId: 4,
     title: "Hello my friends", 
     text: "To all my friends Hello",
     description: "again"
  }
];

Array of keys:

const keys = ['title', 'text'];

Search term:

const searchTerm: 'Hello';

Output should be:

const filteredArray = [
   {
     postId: 1,
     title: "Hello my friends", 
     text: "To all my friends",
     description: "Random text"
   },
   {
     postId: 2,
     title: "To all my friends", 
     text: "Hello my friends",
     description: "Hello text"
   },
   {
     postId: 4,
     title: "Hello my friends", 
     text: "To all my friends Hello",
     description: "again"
   }
];

Only the third object should not be in filtered array in this example.

I tried this approach: https://jsfiddle.net/pnc75tzg/

I have duplicates inside array. I guess I could check if that object already exists in array before push.

But overall I don't think this would be a good approach. Any idea on what would be a good approach to filter this the right way?

CodePudding user response:

You can use filter combined with some here. Since only 1 of the keys satisfying the condition is enough some can be used. If all the keys should have the search string use every instead of some.

some returns true if at least 1 element in the keys array satisfies the condition i.e the string 'Hello' is included in the corresponding value of the key.
On the other hand every returns true only if all the elements satisfy the condition

const posts = [  {     postId: 1,     title: "Hello my friends",      text: "To all my friends",     description: "Random text"  },  {     postId: 2,     title: "To all my friends",      text: "Hello my friends",     description: "Hello text"  },  {     postId: 3,title: "My friends",      text: "To all my friends",     description: "Random Hello",  },  {     postId: 4,     title: "Hello my friends",      text: "To all my friends Hello",     description: "again"  }];

const keys = ['title', 'text'];
const searchTerm = 'Hello';

const filteredPosts = posts.filter((post) => keys.some((key) => post[key].includes(searchTerm)))

console.log(filteredPosts)

if all the keys need to satisfy

const posts = [  {     postId: 1,     title: "Hello my friends",      text: "To all my friends",     description: "Random text"  },  {     postId: 2,     title: "To all my friends",      text: "Hello my friends",     description: "Hello text"  },  {     postId: 3,title: "My friends",      text: "To all my friends",     description: "Random Hello",  },  {     postId: 4,     title: "Hello my friends",      text: "To all my friends Hello",     description: "again"  }];

const keys = ['title', 'text'];
const searchTerm = 'Hello';

const filteredPosts = posts.filter((post) => keys.every((key) => post[key].includes(searchTerm)))

console.log(filteredPosts)

  • Related