Whats a cleaner way to write this javascript if statement? [duplicate] - javascript

This question already has answers here:
Check if an array contains any element of another array in JavaScript
(32 answers)
Closed 1 year ago.
I've tried the format like
['mobile-number', 'name', 'about'].includes(stages)
But that doesn't seem to work.
if (stages.includes('mobile-number') || stages.includes('name') || stages.includes('about')) {
array.push('about-you')
}

Your looking for Array.prototype.some
stages.some(s => ['mobile-number', 'name', 'about'].includes(s))
This is the exact alternative of stages.includes('mobile-number') || stages.includes('name') || stages.includes('about')

Related

Find and delete cookie with javascript [duplicate]

This question already has answers here:
How to delete a cookie?
(13 answers)
Closed 6 years ago.
How would like to with javascript find a cookie that starts with 'AzureAppProxyAccess' and delete that cookie? It always has a series of random numbers at the end of the name. It is in the same domain so I have access to it.
This is what I have tried with jquery but I would like just javascript.
for (cookie in $.cookie()) {
if(cookie.substring(0, 19) === "AzureAppProxyAccess") {
$.removeCookie(cookie);
}
};
You can use indexOf in strings.
cookie.indexOf('AzureAppProxyAccess') > 1 ? doSomething : somethingElse
Or, like it was said in one of the comments, use RegEx.

How to convert string into boolean? [duplicate]

This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 6 years ago.
I read out a data-attibute, then I want to convert the string "true" into a boolean. At the moment I have to do a comparison in the javascript, is there a better way to do it? I don't know how to use this solution
HTML
<div data-nav='{ "nav": "true"}'>
JS
var data = JSON.parse($nav.attr('data-nav').toString());
data.nav = (data.nav === "true") ? true : false;
Try
<div id='test' data-nav='true'>
var truefalse = $('#test').data('nav');
.data should be able to evaluate it as boolean
here is an example in JSFiddle
https://jsfiddle.net/lismore/hx3gLvgw/

How to check if location.hash has `#access_token=` and if so, do something? [duplicate]

This question already has answers here:
How to check whether a string contains a substring in JavaScript?
(3 answers)
Closed 6 years ago.
Right now, I'm just doing the following for my Auth0 login -
if (location.hash) {
// do something
}
How can I make sure it will only do this when it includes #access_token= ?
Use indexOf like so.
if(location.hash.indexOf("#access_token=" > -1) {
// Do stuff
}
You can learn more on indexOf here.

Is there any clean way to code a group of || testing to see if the value of a variable is in one of them? [duplicate]

This question already has answers here:
Javascript shorthand if
(5 answers)
Closed 9 years ago.
I have code like this:
if (view == 'delete' || view =='edit' || view == 'new') {}
Is there a more clean way that I can code this without using an external library. It just does not look too good to have all the || and maybe more later.
Use indexOf (won't work on ancient IE versions):
if (["delete", "edit", "new"].indexOf( view ) >= 0) { alert("ME");}
You can also use regular expression:
if (/^(delete|edit|new)$/.test(view)) {
// true
}

searching values in array javascript [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.

Categories

Resources