-1

I want to filter array if it contains a particular string. It works as shown below. But I am not sure how to solve it if there are multiple strings to match. For example - instead of skillText = "Entry Level"; if we have skillText = ["Entry Level", "Scientist", "Analyst"];

var myList = [{"Title":"Entry Level - Data Analyst","Location":"Boston"}, {"Title":"Entry Level3 -  Analyst","Location":"Delhi"}, {"Title":"Scientist","Location":"Boston"}];

skillText = "Entry Level";
result = myList.filter(function(v) {
    return v["Title"].indexOf(skillText) > -1;
  })
  
console.log(result);  

For exact match skillText.includes(v["Title"]) works but not sure how to do with partial matches.

Ujjawal Bhandari
  • 1,333
  • 1
  • 8
  • 16
  • 1
    Does this answer your question? [Filter array of objects based on another array in javascript](https://stackoverflow.com/questions/46894352/filter-array-of-objects-based-on-another-array-in-javascript) – pilchard Mar 27 '22 at 14:16
  • No it does not. As stated in the post I already know how to use includes( ) but don't know how to do it when partial matches. – Ujjawal Bhandari Mar 27 '22 at 14:18
  • Then this: [Filter object array based on string array "with partial match"](https://stackoverflow.com/questions/58250401/filter-object-array-based-on-string-array-with-partial-match) – pilchard Mar 27 '22 at 14:25
  • Also see: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – pilchard Mar 27 '22 at 14:25
  • Essentially, `skillText` may be a string - but it may also be an array of strings. Correct? – jsN00b Mar 27 '22 at 14:36
  • @jsN00b Yes, correct – Ujjawal Bhandari Mar 27 '22 at 14:41
  • 1
    @jsN00b it doesn't really matter, one can just `[].concat(skillText).some(...` so that it's always an array. – pilchard Mar 27 '22 at 14:43

2 Answers2

3

You can use the some function on arrays, it returns true if any of the items returns true from the callback

skillTexts = ["Entry Level", "Scientist"];
result = myList.filter(function(v) {
    return skillTexts.some(function(skill) {
        return v["Title"].includes(skill)
    });
})
Luminight
  • 91
  • 4
1

This may be one possible solution to achieve the desired objective:

Code Snippet

const findMatches = (hayStack, needle) => (
  hayStack.filter(
    ({Title}) => (
      [].concat(
        needle
      ).some(
        skill => Title.includes(skill)
      )
    )
  )
);

const myList = [{"Title":"Entry Level - Data Analyst","Location":"Boston"}, {"Title":"Entry Level3 -  Analyst","Location":"Delhi"}, {"Title":"Scientist","Location":"Boston"}];

let skillText = 'Entry Level';
console.log('searching text: "Entry Level"\nfound: ', findMatches(myList, skillText));

skillText = ['Level', 'Analyst', 'Scientist'];
console.log('searching array', skillText.join(), '\nfound: ', findMatches(myList, skillText));

Explanation

  • Use .filter to iterate over the array (hayStack)
  • De-structure to directly access Title from the iterator
  • To acccommodate needle (search-text being a string or an array), first use [].concat() to obtain an array (This is done per pilchard's comment in the question above)
  • Use .some to iterate and find if any element is part of Title
jsN00b
  • 3,584
  • 2
  • 8
  • 21