ManuKaracho ManuKaracho - 2 months ago 7
AngularJS Question

How to match similar strings in angular

I have to arrays that contain names of premier league players.
I want to match them by name since the player objects don't have unique ids.

How can I make a string comparison that will match

Zlatan Ibrahimovic
with
Zlatan Ibrahimović
for example? (notice the last character of both strings)

Answer

It is not a trivial problem. You should look into Levenshtein distance problem

https://en.wikipedia.org/wiki/Levenshtein_distance

You can search in google for different implementations or use a library like: https://www.npmjs.com/package/levenshtein

Example:

l = new Levenshtein( 'Zlatan Ibrahimovic', 'Zlatan Ibrahimović')
// l === 1

I used already, and I liked. In my code, I used this one for an experimental proposed.

I don;t care about the result. Because in a long string 4 can be a very good number and in small one 2 it is very bad.

I get to do something like l/Math.max(str1.length, str2.length) then you can make your number and decide wich number is interesting for you.

Comments