Determine whether the elements in an array are contained within a string as substrings

Is there a way to search through an array and return if its value is a substring of a string?

An example in SQL would be: LIKE '%arr.value%'

Let's say we have an array, arr = ['welcome', 'answer', 'question'], and a string, str = 'welcome001'.

Answer №1

Indeed, you have the option to utilize String.prototype.includes() and Array.prototype.find() for discovering your desired string

    let words = ['hello', 'goodbye', 'salutations']
    let phrase = 'goodbye123';
    const outcome = words.find(w => phrase.includes(w));
    
    console.log(outcome);

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

Off-center map display on Google Maps result

I am utilizing a listener that triggers the display of a div each time a marker is clicked: google.maps.event.addListener(marker, 'click', function() { var boxText = document.createElement("div"); boxText.style.cssText = "margin-top: 8px ...

I require the ability to execute a JavaScript or jQuery script within a PHP environment

To update the page and dynamically populate the second dropdown based on the selection in the first dropdown, follow these steps: Use PHP to select the dropdown value, triggering the JavaScript function `reloadproject()` to refresh the page with the sele ...

Submitting a form is disabled when there are multiple React form inputs

I have a simple code example that is working correctly as expected. You can check it out here: https://jsfiddle.net/x1suxu9h/ var Hello = React.createClass({ getInitialState: function() { return { msg: '' } }, onSubmit: function(e) { ...

Develop a dynamic daily messaging system

I'm interested in incorporating a daily changing message feature on my website. My plan is to store all the messages in a MySQL database and have them displayed daily. Any guidance on how to achieve this would be highly appreciated! Thank you, I ha ...

Guide to setting up a lobby system with Javascript within a Django framework

I am looking to develop a lobby system where users can create rooms, other users can join the room, and the creator of the room will select 9 participants to form 2 teams of 5 players each. Once both teams are finalized, the creator will close the room wit ...

JavaScript Closures can utilize a shared global setting object or be passed as an argument to individual functions

Looking for advice on the use of a global "settings object" in relation to JavaScript closures. Should I access it from within functions in the global scope or pass the object every time a function requires access? To provide context, here's a mockup ...

I am unable to pass my AJAX post variable to my PHP script

I successfully created an AJAX function, but I am facing an issue with retrieving the variable using PHP. Below is my code: // Validate signup function validateSignup(){ // Get values var username = document.getElementById("pm_username").value; ...

Replicating the performance graph of Windows Task Manager

Looking for a JavaScript library that can replicate the dynamic chart seen on the CPU Usage History section of the Windows Task Manager's Performance tab. Any recommendations would be highly appreciated. ...

FireFox is unresponsive to OPTIONS requests

I have a webpage that is accessed through HTTP. The client-side code is making AJAX requests for authorization to the same domain, but using HTTPS, which causes CORS issues. When using FireFox, the request looks like this: // domains and cookies are chang ...

When utilizing DomSanitizer, Angular2 suddenly ceases to function properly

I've been working with Angular 2 and TypeScript. Everything was going well until I encountered an issue with my pipe, which is causing the DomSanitizer to interfere with the (click) event functionality. Even though the (click) code appears intact in ...

The root styles are failing to be implemented in an angular-cli project

I am currently working on a project using angular-cli and SCSS. I have noticed that while other SCSS files are successfully compiled and changes are visible, styles.css and app.component.scss in the root path remain uncompiled and their changes cannot be o ...

Pull information from a URL using JavaScript and present it within a PHP document

When an incorrect password or captcha is entered, an error message appears in the URL. The error on the URL will look something like this: www.domain.com/signup.php?error_msg=The+characters+you+entered+did+not+match+the+word+verification What is the best ...

Having trouble with Fancybox Loading On Page Start function not working properly in Internet Explorer?

My website has a Fancybox that is supposed to load as soon as the page loads. It works perfectly in Firefox, but unfortunately it does not work in Internet Explorer and I am unsure why. There are no errors displayed in IE when the page loads, it simply doe ...

Rendering Radial Gradients Using CSS3 in Google Chrome

While experimenting with animating radial gradients using jQuery, I came across an interesting observation (take a look at this JSFiddle). As I moved the mouse pointer over the left side of the element, the position animation appeared smooth. However, movi ...

hover-over images

Hello there! I'm currently experimenting with simple rollover images for a website that I am working on. However, I've noticed that when I try the effect in Firefox, it seems to move other elements on the webpage. Do you have any suggestions on h ...

Maintain Ajax content even when the back button is pressed

Currently, I am facing an issue with my product page that has infinite scroll pagination and loads more products using Ajax. For example, the URL is: domain/books. When a user clicks on a product, they are directed to the specific product page at: domain ...

Issues arise with the functionality of webcams in conjunction with JavaScript on select devices

I am currently working on developing an Electron application using Vue.js that incorporates a webcam feature. Interestingly, the webcam functions perfectly within the Electron application on one computer but only displays a black screen on another computer ...

Having trouble confirming signature with Node.js Crypto library, specifically with key pairs

I have a concise nodejs code snippet where I attempt to sign a string and then verify it using node crypto along with key pairs generated through openssl. Despite trying various methods, the result consistently shows "false" indicating that the signature c ...

Troubleshooting a malfunctioning custom filter in AngularJS

I'm having trouble implementing a custom filter in AngularJS. The output is not what I expected. Here is the code I have: script.js var myApp = angular.module('myModule', []); myApp.filter("gender", function(){ return function(gender){ ...

Accessing Data from JSON with AJAX

Looking for help with JSON and AJAX? I need guidance on how to Retrieve JSON value using AJAX. This is my json file named list.js { "loginid" : "Wafiqa", "password": "123" } and this is my html file named ajaxTest.html <body> <p ...