problems with using includes() in the array every() method

Hi everyone, I've been spending quite a bit of time trying to find a solution to this problem and I'm hoping someone can help me out.

The issue at hand is that I have 2 arrays - one containing multiple words from an address and another built with words entered in an input field. The current setup returns true only if the exact same word is entered in the input as it appears in the string array. However, I would like it to return true if there is a partial match as well. For example, if the string array contains "CENTER" and "RACHEL", entering "CEN EL" in the input should also return true because it partially matches 2 elements in the array.

Here's the code snippet I've put together:

function checker(arr, words) {
    return words.every(v => arr.includes(v));
}

this.shippingAddress.forEach(address => {
    const stringBuild = `${address.name} ${address.display_address} ${address.phone}`;
    const arrayString = stringBuild.split(' ');
    if (checker(arrayString, words)) {
        _results.push(address);
    }
});

An example of input for the array 'arrayString':

[
    0: "CENTER"
    1: "MANIKI"
    2: "BRUCHESI"
    3: "2225,"
    4: ""
    5: "STREET"
    6: "RACHEL"
    7: "EAST,"
    8: "CITY"
    9: "STUFF,"
    10: "STUFF"
    11: "STUFF"
    12: "STUFF"
    13: "STUFF"
]

For the array 'words':

[
    0: "CEN"
    1: "EL"
    
]

The expected output would be true because 'CEN' and 'EL' are included in the first array when passed through the checker function, even though they are not full words.

Answer №1

Are you looking to determine if every word in the array has some level of match, whether partial or complete?

To achieve this, I suggest iterating through each word that needs to be matched and checking if any of the typed words partially matches the target. A regex can be used for this purpose, with a case-insensitive flag set using "i":

const compareWords = ["apple","orange"];

const isValid = (typedWords) => compareWords.every( word1 => typedWords.some( word2 => new RegExp(word2, "i").test(word1)))

console.log(isValid([ "app" , "ora" ]))
console.log(isValid([ "ban" , "pear" ]))

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

Encountering a 404 error while trying to delete using jQuery

I have been attempting to execute a 'DELETE' call on an API, but so far I have had no success. Despite being able to access the URI and view the JSON data, my DELETE request does not work. Interestingly, other web services are able to perform thi ...

Steps to create a continuous blinking/flickering effect on a highchart pathfill

I am currently utilizing highcharts within one of my applications. I want to emphasize a particular stroke based on the content, and while I have achieved this already, I now need it to blink or flicker as if indicating an issue at that specific point. C ...

How can I insert my URL into the webPDFLoader feature of LangChain platform?

I need help figuring out how to load a PDF from a URL using the webPDFLoader. Can someone explain how to implement this? Any assistance would be greatly appreciated. I am working on this task in nextjs. Where should I place the pdfUrl variable within the ...

Utilizing React-map-gl in combination with either useContext or useState to update the viewport from a separate component

Lately, I've been struggling to understand how to use useContext and/or useState with React/Nextjs along with react-map-gl. In my project, I have a searchbox component that uses Formik and a map component from react-map-gl. What I'm trying to ach ...

The authentication protocol, Next Auth, consistently provides a 200 status response for the signIn function

I ran into a challenge while building my custom login page using Next Auth. The issue arises when I try to handle incorrect login data. If the login credentials are correct, I am able to successfully send JWT and redirect to /dashboard. However, when the l ...

Trick to enable editing in Bootstrap Select Combobox

Is there a way for users to add their own options to bootstrap-select? Greetings! I have spent some time searching for a straightforward solution that is compatible with Bootstrap 4 styling. Despite exploring various suggestions, as well as unresolved thr ...

Clicking on the ajax tab control in asp.net displays a single rectangular box - how can this be removed?

When using tab control in Ajax, I encountered an issue where a blue rectangle box appeared when clicking or opening the page. How can I remove this unwanted box? ...

What is the best way to display the original array when the search input for the title is left blank?

When I enter a todo item title in the search input field and then clear the search input, I expect the initial array of todos to be rendered again. I attempted to accomplish this using an if statement but it did not work - only the previously searched todo ...

Shifting the entire page from left to right and back again

I am in search for a template that moves the page from left to right. If anyone can guide me on how to create this effect or provide a JavaScript example, I would greatly appreciate it. ...

Transferring information among various instances of a single controller (ng-controller)

I am relatively new to using Angular and I am facing a challenge with a seemingly simple task that is proving to be more complex within the framework. The issue at hand involves data manipulation, specifically with a variable named var1. I am modifying th ...

JavaScript and HTML - specify the location in the html document where the JavaScript script will be displayed

I'm a beginner when it comes to JavaScript. I am trying to make sure that my HTML page remains unchanged while JavaScript text is displayed in a specific location without refreshing the entire page. To trigger a JavaScript function via a button on a ...

Utilize various CSS styles for text wrapping

I'm struggling to figure out where to begin with this problem. My goal is to create a three-column row that can wrap below each other if they cannot fit horizontally. The height of the row should adjust to accommodate the items while maintaining a fix ...

Can someone explain why the index call is not reaching the "/" route in Node.js?

When I access the server by going to http://localhost:3000, it displays my index file but does not route to the get function in index.js. I am currently stuck at this point. Below is my app.js file: var express = require('express'); var path = ...

Utilize Node.js to proxy Angular requests to a service hosted on Azurewebsites

I am trying to set up a proxy post request in my Node.js server and receive a response from the target of this request. Below is an excerpt from my server.js file code where I have implemented the proxy, but I am facing a issue with not receiving any respo ...

Execute the following onclick event when the button is enabled

I am trying to display a popup when the "add to cart" button is clicked (but only when the button is active!). I thought about using a div that wraps the button and changes the class from "disabled" to "enabled". I have tried using jQuery, but it could als ...

Organizing textual maps within a 2D array for rendering on an HTML5 Canvas

In my spare time, I am working on creating an HTML5 RPG game. The map is implemented using a <canvas> element with specific dimensions (512px width, 352px height | 16 tiles across, 11 tiles from top to bottom), and I'm wondering if there's ...

What is causing my array of objects to constantly accumulate undefined elements?

My quick sort function implementation for the object users_total_likes is behaving unexpectedly. When compiled and run in the terminal or browser, it adds undefined values causing a TypeError: if(users[i][key] >= users[hi][key] && users[j][key] ...

At times, jQuery cubes can occasionally overlap each other

Recently, I encountered an issue with my Pinterest-style website where the cubes were overlapping on page load, even though my jQuery script was designed to evenly space them regardless of browser size. After discussing with the developer who helped me cre ...

.npmignore failing to exclude certain files from npm package

I'm facing an issue with a private module on Github that I am adding to my project using npm. Despite having a .npmignore file in the module, the files specified are not being ignored when I install or update it. Here is what my project's packag ...

"Utilizing jQuery to apply a class based on the attributes of CSS

Is there a way in jQuery (or plain JS) to create a condition based on whether a div has a specific CSS attribute? For instance, I need jQuery to apply position:fixed to an element's CSS when another element has display:none, but switch back to positi ...