Tips for comparing strings that are nearly identical

Looking to filter elements from an array based on partial matching of a string. For example, trying to match PGVF.NonSubmit.Action with NonSubmit by checking if the string contains certain keywords. The current code is not functioning as expected and only matches complete string.

Below is the existing code:

for (let i = 0; i < appNames.length; i++)
{
   var strToMatch = appNames[i];
   let obj = products.filter(x => strToMatch.includes(x.Name));
}

The goal is for obj to receive multiple records that match partially. This needs to be done within the loop due to other operations that need to be performed. Any suggestions on how to achieve this?

Answer №1

To find a specific word within a string, you can utilize the match() function

Finding "NonSubmit" in "PGVF.NonSubmit.Action"

console.log("PGVF.NonSubmit.Action".match("NonSubmit"))

>>>[ 'NonSubmit', index: 5, input: 'PGVF.NonSubmit.Action', groups: undefined ]

Finding "NoSubmit" in "PGVF.NonSubmit.Action"

console.log("PGVF.NonSubmit.Action".match("NoSubmit"))

>>>null

Answer №2

Resolved the issue with the following code snippet

const matchedItems = Object.values(items).filter(item => item.Title.toString().includes(searchString));

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

Tips for modifying JSON property names during the parsing process

As outlined in the JSON.parse documentation, a reviver function can be utilized to modify the value of each property within the JSON data. Here is an example: JSON.parse('{"FirstNum": 1, "SecondNum": 2, "ThirdNum": 3}', function(k, v) { return ...

Error in Jquery: Unable to locate element with attribute "[data-weight]"

After removing a lot of unnecessary code for better readability, I encountered an error on this line: $('[data-weight]').each(function() { The error message indicates that it is null <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "h ...

Inject JSON data into a JavaScript array

I am dealing with a JSON file in the following format: [{"excursionDay":"2"},{"excursionDay":"3"},{"excursionDay":"4"}] My goal is to extract the values of excursionDay and store them in an array in JavaScript like this: dayValues = [2,3,4] Here is m ...

Overlay a translucent image on top of another using JavaScript

Is it feasible to overlay a transparent-background image onto another image using JavaScript? For example, if you have a website featuring various product pictures and some of these products are recalled, can you superimpose the Do-Not-Enter sign (circle-w ...

Issue: The plugin 0 mentioned in the file "/my dir/node_modules/babel-preset-php/src/index.js" contains an invalid property called "default"

While attempting to convert a PHP script to JavaScript using babel-preset-php, I encountered the following error: Error: Plugin 0 specified in "/media/deep/5738c180-2397-451b-b0b5-df09b7ad951e1/deepx/Documents/TestingAll/node_modules/babel-preset-php/ ...

Transforming JSON objects, retrieve an empty value in place of undefined

Can someone please offer some advice on the following: I am retrieving JSON data using this code snippet: $.getJSON(jsonPath, function(returnedData){ ... }); The JSON object returned will have a structure similar to this: ... "skin": { "elapsedTextCo ...

ThymeLeaf fails to display the elements of a List

I am struggling with a complex HTML structure in my project. The HTML code includes a mix of Bootstrap classes and Thymeleaf attributes. Additionally, there is a JavaScript function that interacts with radio buttons to show or hide elements based on user i ...

Choosing an element beneath a table row using a different element as a reference

I'm attempting to identify the checkboxes associated with the link containing delete.jpg. Here's what I've tried so far: <table> <tr class="odd"> <td><input id="cbox1" type="checkbox"></input></td> ...

Exploring the Big O complexity of QuickSort in JavaScript

My experience with QuickSort involved testing two algorithms on LeetCode for a specific problem. Surprisingly, both algorithms worked, but one outperformed the other significantly, leaving me puzzled about the reason behind the speed difference. One of th ...

"Troubles encountered while trying to save JSON information on NodeJS Express platform

I've encountered an issue with saving and accessing my JSON data - all objects in the JSON file are being converted to strings, even numbers. Below is my express route for retrieving data from an HTML form: control.post('/like/:id', getuser ...

Unable to update due to outdated response call causing issues

I am currently in the process of updating outdated response calls and have encountered a peculiar issue where the response is not being properly ended. Typically, I would use : res.send(200, {message: 'Location Updated'}); However, this method ...

Forwarding from a user interface element in Next.JS

I am currently working on a project utilizing Next.js 13, and I have encountered a situation where I need to invoke a client-side component from a server-side page. The specific component in question is the DeleteAddressAlertDialog which interacts with my ...

Employing regular expressions within Notepad++ to insert whole numbers into img src within table elements

I have a large table filled with numerous items that require individual images to be added. Let's take a look at the code snippet below. <tr><td><img src=".jpg" width=100 height=100/></td></tr> Although I could manually ...

Utilizing vue-chartjs to display a pair of data figures side by side

In my vue application, I utilize vue-chartjs to showcase some data and I incorporate semantic-ui for the layout. My goal is to display two figures side by side on the screen, but unfortunately, I am facing difficulties achieving this. I have attempted us ...

When using ESLint together with React, you may encounter errors related to `no-unused-vars

I've configured eslint and eslint-plugin-react. After running ESLint, I'm encountering no-unused-vars errors for all React components. It seems that the linter is not recognizing JSX or React syntax. Any solutions? For example: app.js import ...

Getting the result from HTML5 FileReader: How does it work?

Dealing with the asynchronous nature of JS FileReader can be challenging. I need to retrieve the result after reading a file and work with that data, but I don't know when the result will be ready. How can I handle this situation effectively? $(docum ...

Retrieve only the items from a JavaScript array where the index is below a specified value

My goal is to filter out all the items from the initialItems list that have an index lower than the current item. For example, if the current item is CM, I want to display QS, IT, and AB in a draggable dropdown menu. However, I'm unsure of how to prop ...

What are some ways to reinvigorate your determination?

With the use of ui-router, a state is created with a resolve function: .state('tab.social', { url: '/social/', views: { 'menuContent': { templateUrl: 'templates/social/tab-social.html', ...

Relocating scripts that have already been loaded

When using AJAX to load a page, the entire content including <html>, <head>, <body> is loaded. This means that all scripts meant to run on page load will be called. However, sometimes the browser may remember that certain scripts have alr ...

exploring the contrast of css versus javascript selectors

Could you please explain the contrast between div#name and #name? Is there a significant difference when using class or id to position an element? Thank you for your help. ...