Simplified syntax for conditionals or in comparison to multiple strings

This is what I currently have:

if(this.selectedItem.label == "str1"
   || this.selectedItem.label == "str2"
   || this.selectedItem.label == "str3"
   || this.selectedItem.label == "str4") {
}

I am curious if there is a more concise way to refer to "this.selectedItem.label" only once.

Answer №1

Could it be an array and potentially the indexOf function being used here?

if(["str1","str2","str3","str4"].indexOf(this.selectedItem.label) > -1){
  // found
}

This is a solution that should work across different browsers.

Alternatively, consider using includes (although I haven't tested it in IE)

if(["str1","str2","str3","str4"].includes(this.selectedItem.label)){

}

Answer №2

if(["string1", "string2"].indexOf(this.selectedItem.label) !== -1) {
   // Add your code here 
}

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

Encountered an eas error during Android build process

When I try to execute the command "eas build --platform android" I encounter the following error message: "✖ Build failed ...

The CoffeeScript closure _this reference becomes elusive when trapped within multiple nested loops

Something interesting to note about CoffeeScript. Summary: { In CoffeeScript, the fat arrow (=>) creates a closure that stores the reference to `this`. Every instance of @ is replaced with the original value of `this`. For example, the following code: ...

Using the return value from a callback function in jQuery

Let's get started, I created an Ajax namespace in JavaScript to simplify remembering the required values for it to function properly. (function (ns) { ns.type = "POST"; ns.url = "/"; ns.data = {}; ns.success = function() {}; ns. ...

Display dynamically generated array elements using the Highcharts.js library

Check out my demo on: DEMO This fiddle represents a simulation of gathering data from sensors using MQTT. The dataArray[] is updated every second. By clicking a button, an element is added to the array at each click. The graph plotting starts after a 5- ...

A missing Array.at() method has been reported in TypeScript array type

When I try: const myArray = [0,4,2]; myArray.at(-1); I encounter the following error related to .at The error message reads: Property 'at' does not exist on type 'number[]'.ts (2339) Why is Array.at() not working in this case? Is th ...

Create a dynamic animation effect for the placeholder in an input field that triggers when the user starts typing

Have you ever come across interactive demos like this one? I've noticed that most examples involve creating a new element. parent().append("<span>" + $input.attr('placeholder') + "</span>"); Is there a way to make the placehol ...

Upgrade Angular from 12 to the latest version 13

I recently attempted to upgrade my Angular project from version 12 to 13 Following the recommendations provided in this link, which outlines the official Angular update process, I made sure to make all the necessary changes. List of dependencies for my p ...

Exploring AngularJS routing integration with Rails

I have recently started incorporating AngularJS into my Rails application. However, I am facing an issue where my route providers with Angular are not functioning as expected. Instead of displaying my template, the Rails view is being displayed. routes.rb ...

how do I modify my JavaScript code to achieve the desired outcome

How can I program a button to disable after being pressed 10 times in less than a minute, but continue counting if not pressed for 1 minute? function buttonClicked() { if (totalCount + accCounter > 9) { document.getElementById("btn").disabled = ...

Ensure that pressing the ENTER key on a form will only activate a specific submit button, even when there are multiple buttons present

I have a form containing two submit buttons - name="submit_button" and name="search_me_logo". Depending on which button is pressed, the form should perform different actions when it reaches the action location. Currently, when the enter key is pressed, th ...

Capturing keydown events exclusively in the topmost layer of the HTML document

Currently, I am developing a web application that includes an underlying HTML file with some JavaScript functionality that I cannot modify. In my code, I create a layer on top of this page using CSS with z-index: 1000;. However, I am facing an issue where ...

Leveraging AJAX for transferring variable from a dynamic HTML table to PHP for executing an update query

Is there a way to insert a value into the input field valor and update the respective row with that value using both the ID and the valor in the update query? I seem to be missing something here, what could it be? Table <?php $IDTipoEquipamento = ...

Display the variance in values present in an array using Node.js

I am facing a challenge and need help with printing arrays that contain both same and different values. I want to compare two arrays and print the values that are present in both arrays in one array, while also creating another array for the differing val ...

Troubleshooting issue with JavaScript promise not functioning properly using resolve() method

I encountered an issue with promise usage in JavaScript. My objective was to retrieve data from firebase, store the results in an array, and then perform sorting on that array. Below is my code snippet: let promise = new Promise((resolve, reject) => ...

Ajax- priorToSend

To avoid encountering the same error twice, I implemented the use of beforeSend in my code. hasSent = false function submit() { if (!hasSent) { $.ajax({ url: "${createLink(controller:'userInvitation', action:'ajaxUp ...

Investigate Angular CLI project using IntelliJ Ultimate for debugging

Seeking help with debugging my Angular project in Intellij Ultimate. I have followed a tutorial but can't seem to get it working. Any ideas on what I might be missing? I am using: - Angular CLI: 1.6.8 - Node: 6.10.0 - OS: win32 x64 - Angular: 5.2.4 ...

What is the process of invoking a function on a specific element when it is encapsulated within an if statement in Meteor.js

Here is an example: {{#if currentUser}} <li><a class="waves-effect waves-light btn modal-trigger modal-close" href="#upload">Upload Image</a></li> {{/if}} Currently, I am implementing the following: Template.MasterLayout.onRe ...

Why is the background image not loading properly on first hover with CSS?

Whenever we load a page with a large image set as the background for a div, there seems to be a delay in displaying the image when we hover over the div. Is there a solution to this issue? I would prefer not to use a sprite image because I need to make alt ...

Override existing Keywords (change false to true)

Are there any ways to overwrite reserved words? It's not something I would typically consider, but it has sparked my curiosity. Is it feasible to set false = true in JavaScript? I've come across instances on different websites where individuals ...

ui-sref unable to access controller information or view

I'm experiencing some difficulty accessing the controller for my state param. I've made sure to use the correct state to link to the next view. <td><a ui-sref="orders({customerId: cust.id})">View Orders</a></td> In my c ...