Exploring the Versatility of the IN Operator in Jquery and Javascript

RatecardIDs=[2, 22, 23, 25];
 if (parseInt(cellValue) in (RatecardIDs)) {
}

Is it possible to use the In operator in an if condition? This code first executes the code in the if block, but then it goes on to the else block.

Answer №1

The in operator in JavaScript is used to check for keys in objects.

To determine if an element exists in an array, you can utilize Array.indexOf(). If the element is not found, it will return -1.

Now, you can verify using

if(RatecardIDs.indexOf(parseInt(cellValue))>-1) {...}

Answer №2

The in operator in JavaScript is utilized to retrieve the keys within an object. To achieve this, you can opt for JavaScript's Array.indexOf or jQuery's inArray.

Implementation is shown below:

RatecardIDs = [2, 22, 23, 25];

if (RatecardIDs.indexOf(parseInt(cellValue)) > -1) {
 ...
}

Alternatively, jQuery can also be used:

if ($.inArray(parseInt(cellValue), RatecardsId) > -1) {
  ....
}

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

Determine whether an array is empty using PHP

In my PHP code, I am working with an array and need to determine if it is empty or not. $length = count($array_new); if(empty($array_new)) echo("Array is empty"); else echo("Array is not empty"); echo("\n"); print_r($array_new); echo("Length ...

Combine the elements within the HEAD tag into a group

I am currently developing a dynamic page system using AJAX. Whenever a link is clicked, an AJAX call is made to retrieve data from the requested page. However, I have encountered an issue where certain data in the page file needs to be placed within the h ...

Is there a way to customize the look of the time selected in the <input matInput type="time" step="1" /> time picker?

Currently, I am utilizing the following code as a time picker in my Angular 9 application. My goal is to modify the selected time's color to a bright blue shade. How can I accomplish this task? <input matInput type="time" step="1&quo ...

Listening to changes in a URL using JQuery

Is there a way to detect when the browser URL has been modified? I am facing the following situation: On my webpage, I have an iframe that changes its content and updates the browser's URL through JavaScript when a user interacts with it. However, no ...

Creating custom ExpectedConditions with Protractor for detecting attribute changes

I've been working on creating a custom ExpectedConditions method that can wait for an element attribute to change. Here is the approach I came up with: const CustomExpectedCondition = function() { /** * Check if element's attribute matches ...

The exported NextJS URL isn't functioning properly

Recently, I delved into the world of Next JS by following a tutorial on YouTube by Brad Traversy. In his guidance, I used next export to export the program and ran it using serve -s out -p 8000. While the page loads perfectly on localhost:8000, the issue a ...

The initial attempt at using Ajax inside onbeforeunload is unsuccessful

I have attempted to attach the beforeunload event by executing the subsequent script in order to use AJAX to navigate to a specific URL. However, I am encountering an issue where AJAX does not work the first time when I refresh the page, as the URL is no ...

What is the best way to assign attributes to multiple HTML elements using an array?

Seeking assistance to hide various sections in my HTML file upon button click, with the exception of one specific section. Encountered an error message: Uncaught TypeError: arr[i].setAttribute is not a function Here's a snippet of my code: const hide ...

What is the best way to split a one-dimensional array when a certain condition is satisfied?

I have a task to input an array of integers. What I would like is to allow the user to stop entering numbers once they have entered all the required numbers and move on to displaying the input numbers. Below is the code that I have written for this purpo ...

Modifying CSS files in real-time

I've been attempting to dynamically change the CSS file, but I've run into an issue: Whenever I try to alter the style, it seems to work correctly while in "debug mode", showing that the changes are applied. However, once the JavaScript function ...

obtain the final result once the for loop has finished executing in Node.js and JavaScript

There is a function that returns an array of strings. async GetAllPermissonsByRoles(id) { let model: string[] = []; try { id.forEach(async (role) => { let permission = await RolePermissionModel.find({ roleId: role._id }) ...

Invoke a web service upon the loading of a page within the scope of an AngularJS Framework

My webservice returns a JsonObject and I am calling it in JS using Ajax as shown below : $scope.init = function () { $.ajax({ type: 'GET', timeout: 1000000000, url: serv ...

Issue with ng-hide logic malfunctioning

I am currently developing an Ionic application and encountering some issues with the ng-hide directive. My goal is to display or hide a button based on whether the user has completed registration. The button in question: <button class="button button-c ...

What is the best way to pass the JWT token to the subsequent page once a user has successfully logged in on Next.js?

Introduction I am currently working on a login application using Nextjs for the frontend and Spring Boot for the backend. Issue The problem I am facing is that after successfully logging in from the frontend, which calls the /authenticate login API devel ...

Incorporating image hyperlinks onto a designated webpage within a JavaScript presentation carousel

Working on an e-commerce website, the client has requested 3 slide shows to run simultaneously displaying specials or promotions. I have successfully set up the 3 slide shows next to each other, but I'm unsure how to incorporate image links that direc ...

Exploring the data-* attribute in jQuery

Currently, I have a form structured as follows: <div data-role="page" data-last-value="43" data-hidden="true" data-bind='attr : {"name":"John"}'></div> My objective is to modify the name attribute from "John" to "Johnny". I am att ...

Utilize load-grunt-configs and run bower.install() with grunt

I have been attempting to break down my current Gruntfile into smaller parts for better clarity. However, I have encountered a hurdle while using bower.install to manage project dependencies. My directory structure appears as follows: package.json bower.j ...

How can you display select elements from an array in Smalltalk?

Currently, I am developing a beginner's small talk program with the objective of displaying all elements of an integer array forwards and backwards. Additionally, I intend to print only those array elements that end with a specific digit. Although I ...

Collect form input values in an HTML form and pass them to a PHP array. It is important

Here is an example of a form I am working with: <form data-ajax="false" name="frm" action="cmd_go.php" method="post" > <input type="hidden" name="cmd" value="insertarlineapedidomultiple"> <input type="hidden" name="mod" value="yes"> &l ...

Automated tasks running on Firebase Cloud Functions with cron scheduling

There are two cloud functions in use: The first cloud function is used to set or update an existing scheduled job The second one is for canceling an existing scheduled job The management of scheduling jobs is done using import * as schedule from &ap ...