A guide to finding identical strings in JavaScript

I've got two arrays with the names of premier league players. I'm trying to connect them based on name as the player objects lack unique identifiers.

Any tips on how I can perform a string comparison that will correctly match Zlatan Ibrahimovic with Zlatan Ibrahimović? (pay attention to the last character in both strings)

Answer №1

If you're facing a complex issue, don't underestimate it. Take a look at the Levenshtein distance problem.

For more information, check out: https://en.wikipedia.org/wiki/Levenshtein_distance

You can find various implementations by searching on Google or use a library like this one: https://www.npmjs.com/package/levenshtein

Here's an example:

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

I've personally tried it and found it effective. I incorporated it into my code for an experimental purpose.

The end result may not always be straightforward. In a longer string, a difference of 4 could be significant, while in a shorter one, a difference of 2 might be less desirable.

To make a decision based on your specific case, you could try something like dividing the Levenshtein value by the maximum length of the strings, allowing you to determine which number is more meaningful for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The event "click .className" within the Marionette module is failing to trigger

I'm facing an issue with some code that I've written. Here's a snippet of what it looks like: myApp.module(args, function(args){ Views.MainView = Marionette.ItemView.extend({ //template, tagName, className down: false, events: ...

What is the best way to retrieve dynamic content from a .txt file and have it displayed on multiple HTML pages as they are loaded dynamically?

I have a file named 'm_data.txt' stored on the server that contains a continuous total of 77 (for instance). The total gets updated via a push.php page that writes to the .txt file. <!DOCTYPE html> <html> <head> <title> ...

Cannot access Nextjs Query Parameters props within the componentDidMount() lifecycle method

I have been facing a challenge with my Next.js and React setup for quite some time now. In my Next.js pages, I have dynamic page [postid].js structured as shown below: import Layout from "../../components/layout"; import { useRouter } from "next/router"; ...

How to retrieve email input using SweetAlert2 in PHP?

Hello there! I'm curious about the most effective method for integrating PHP with Javascript. My goal is to execute some coding tasks once an email address has been entered. swal({ type: "success", title: "Congrats!", text: "Please enter your P ...

What is the best way to initialize an array of char pointers in C++?

Pointers are a bit unfamiliar to me, but I have an assignment that specifically asks for an array of pointers of type char. Despite my efforts, I'm struggling to make the code work. #include <iostream> using namespace std; int main() { int ...

Video background in JavaScript

Can anyone help me with using the jQuery plug in videoBG to fill a top div with a video that has 100% width and height? I want the #top-video to fill the #top-content but nothing is showing up. Any suggestions would be appreciated. Thanks! <!DOCTYPE ...

The button is not displaying

When trying to click on the "Open device Access" button within the accordion, it does not appear. It seems like there may be an issue with the JavaScript as the transition from "display none" to "display block" is not functioning properly. The "Open device ...

NestJs does not handle the JSON request body

After setting up a NestJs controller and a facade service for handling POST requests, I encountered an issue where the functionality only worked for content-type "text/plain" and not for "application/json", even though the body of the request was identical ...

Troubleshooting the issue with rotating Three.js boxes towards the mouse pointer

I am facing an issue with rotating several randomly generated boxes towards the mouse position. Even after trying to use lookAt(mouse3D) method, the boxes do not change their rotation as expected. My goal is to have the boxes rotate towards the mouse posit ...

Obtaining a Byte[] from an IntPtr in C#

I am dealing with a .dll file (not created by me) that contains a delegate. The Callback function for this delegate is defined as: "CallBackFN(ushort opCOde, IntPtr payload, uint size, uint localIP)" My question is how can I convert IntPtr to Byte[]? I ...

Encountered an issue with a Node.js POST request leading to an error message: "socket hang up" with code 'ECONNRESET'

After creating a sample to send data to a REST service, I discovered that encountering non-ASCII or non-Latin characters (specifically in data.firstName) causes my post request using TEST-REST.js to throw: error: { [Error: socket hang up] code: 'EC ...

When a webpage reload triggers a 404 error, my website's iframe with the source "videoUrl | sanitize" will display

I am attempting to retrieve a videoUrl from a database and set it to the iframe [attr.src] in order to display a YouTube video. It is imperative that the data originates from the database, as there are limitations preventing us from directly uploading and ...

I am facing an issue with properly linking my jQuery

After searching through numerous posts on this website, I have yet to find a solution to my problem. My issue involves trying to implement a simple jQuery function that is not functioning as expected. It appears that the jQuery link may not be properly set ...

The pattern on one of the cube faces is mirrored

When using an image for the cube texture, I have noticed that the image displays correctly on three out of four faces, but appears reversed on the fourth face. Below is the relevant code snippet: // Accessing the container in the DOM var container2=docume ...

Sharing data from parent to child components in Vue.js: A guide to passing references

I need some help with this code snippet HTML Code: <div class="col-xs-4"> <h3 class="text-center">Incomplete task</h3> <div class="well" style="max-height: 300px;overflow: auto;"> <ul id="check-list-box" ...

Implementing Partial Login and Registration Views using AngularJS in conjunction with MVC5 and ASP.NET Identity

Embarking on the journey of creating a Single Page Application with log-in/register functionality using MVC5, ASP.NET Identity, and Angular feels like diving into a vast ocean of web development technologies. Despite being new to this realm, I delved into ...

Calculate a value within a MongoDB field

Hello everyone, I have a document in my collection that looks like this: { Player_Name: Sandeep Nair Player_TotalWeightedPoints: 80 Player_Rank: 23 } I have around 200 similar documents in my collection. The Player_Rank is determined by the total Weighted ...

Route.get() is looking for a callback function, but instead received an [object Promise]

Currently, I am in the process of developing a REST API using express and following the architecture outlined in this particular article. Essentially, the setup involves a router that calls a controller. Let me provide you with an example of how a call is ...

Retrieving images from a server via AJAX requests

As I delve into learning AJAX, I encountered an issue with retrieving an image from my WAMPSERVER www.directory. Within the IMAGES file, there is an image titled logo.png that I'm attempting to access using the following code: function loadXMLDoc() { ...

Replace the anchor tags with a JavaScript array

Looking to replace a simple anchor link with JavaScript array code. We're taking the long route here, and it's not as straightforward as we thought. The scrollTo property isn't working for us; can anyone spot where we might be going wrong? ...