2

So if i have a string that looks like this:

const name = "Matt"

And want to create an array that looks like this:

nameSearchArr = [
0: "M",
1: 'Ma',
2: 'Mat',
3: 'Matt
]

I am trying to get around Firestores no 'full text search' problem by creating an array and using 'array-contains' so i can search a name and while typing in it will match the nameSearchArr. Anyone know the best way to do it? Thank you in advance!

Matt Volonnino
  • 117
  • 1
  • 10
  • it looks like you need to search for all substrings, like here: https://stackoverflow.com/questions/40818769/get-all-substrings-of-a-string-in-javascript/40818823#40818823 – Nina Scholz May 17 '21 at 17:28
  • Is this just for one word or many words at once? – nice_dev May 17 '21 at 17:35
  • It seems to me that with this array you will not get a full text search, but only a "start by" search. For that there are easier solutions, e.g. https://stackoverflow.com/questions/58815751/firebase-if-a-value-includes-a-specific-text-in-database/58816520#58816520 – Renaud Tarnec May 17 '21 at 20:14
  • Whatever you are after, I am almost sure that you don't need to do this. – Redu May 17 '21 at 20:19

2 Answers2

1

Using slice is an elegant approach.

const getStringParts = (word) => {
   const result = [];
   for (let i = 1; i <= word.length; i++) {
      result.push(word.slice(0, i));
   }
   return result;
}

const name = "Matt";
console.log(getStringParts(name));
Ran Turner
  • 14,906
  • 5
  • 47
  • 53
  • Appreciate the help. your function above lets me understand the function below much better! This works perfect so far, thank you! – Matt Volonnino May 17 '21 at 18:09
0

While I doubt this is overall a good solution for the 'full text search' problem, following code could do the trick:

const name = "Matt"

const result = name.split("").map((e, i) => name.slice(0,i+1))

console.log(result)
peter
  • 583
  • 2
  • 11
  • 1
    Definitely not the best for a search but just building small apps with firebase right now and dont have the money for other 3rd party search features. What would be better would be to get every possible combination of letters but then the array would be huge so i figured this search feature would do for now. Thank you so much for you help so far the search works great! appreciate it – Matt Volonnino May 17 '21 at 18:07