This question already has answers here:
jQuery.inArray(), how to use it right?
(20 answers)
Closed 4 years ago.
I want to check if my array contains the Value Input, i want something like that, anyone have an idea on how to do that ?
if(jQuery.inArray($('#ValueInputTitle').val, variableValueInput) !== -1)
{
console.log("is in array");}
else {
console.log("is NOT in array");
}
You can use this,
if(jQuery.inArray($('#ValueInputTitle').val(), variableValueInput) !== -1)
{
console.log("is in array");}
else {
console.log("is NOT in array");
}
Related
This question already has answers here:
Check variable equality against a list of values
(16 answers)
What's the prettiest way to compare one value against multiple values? [duplicate]
(8 answers)
Closed 2 years ago.
i have created an input field but the logical OR operator is not working as intended.
the code is below.
'use strict'
let search = document.getElementById('search');
console.log(search);
let link = document.getElementById('link');
console.log(link);
link.addEventListener('click', doSomething);
function doSomething(ev) {
console.log(ev.type);
console.log(ev.target);
if (search.value == 45 || 35) {
alert('hi');
} else {
alert('you');
}
}
This question already has answers here:
Javascript if statements not working [duplicate]
(3 answers)
Closed 2 years ago.
I'm trying to do the following:
if (x) {
// then this
} else if (calcDifference = 7) && (currentCMSI = 7) { // problem line
// then this
} else if {
// then this
}
Or this:
if (x) {
// then this
} else if (calcDifference = 7 && currentCMSI = 7) { // problem line
// then this
} else if {
// then this
}
But I keep getting errors, what's the correct syntax for comparing if 2 different variables are both true in an else if statement?
Neither!
= is an assignment operator
== is used to compare two variables
So to answer your question, the second suggestion but with==
if (calcDifference == 7 && currentCMSI == 7)
This question already has answers here:
How to check if two arrays are equal with JavaScript? [duplicate]
(16 answers)
Closed 3 years ago.
Hi this is probably an easy question. So I want to make a basic anagram function in Javascript.
The following snippet does not work
anagrams = (phraseOne, phraseTwo) => {
if (phraseOne.split("").sort() === phraseTwo.split("").sort()) {
return true
} else {
return false
}
}
However this does work
anagrams = (phraseOne, phraseTwo) => {
if (phraseOne.split("").sort().join("") === phraseTwo.split("").sort().join("")) {
return true
} else {
return false
}
}
Why? The arrays are identical before you join("") them
That's because strings are compared by value and arrays are compared by reference in JS. You can find out more about comparison here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
This question already has answers here:
Remove array element based on object property
(12 answers)
Closed 5 years ago.
I have an array, and I want to delete elements that have status equal to true (as seen in this part of code)
listArray.forEach((element, index) => {
if (element.status === true) {
listArray.splice(index, 1);
}
});
The problem is that, if, for example, the first, second and third elements have status true, than the second element will not be deleted
Try this:
listArray.filter(element => element.status === false)
You can also do:
listArray.filter(element => !element.status)
try this:
listArray = listArray.filter(element => !element.status);
This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 2 years ago.
I have a condition:
if (item == 'a' || item == 'b' || item == 'c' || item == 'd' || item == 'e') {
// statements
}
How can I reduce the branching? Is there any other way to write this in JavaScript.
You can also use the newer Array.includes
if (['a','b','c','d','e'].includes(item)) {
...
}
Another option (for the very specific case you posted) would be to compare unicode point values using </>
if (item >= 'a' && item <= 'e') {
...
}
Use Array#indexOf method with an array.
if(['a','b','c','d','e'].indexOf(item) > -1){
//.........statements......
}
You can use an array as shown below.
var arr = ['a','b','c','d','e'];
if(arr.indexOf(item) > -1)
{
//statements
}
This would work nicely:
if('abcde'.indexOf(item) > -1) {
...
}
You could also use the newer String.prototype.includes(), supported in ES6.
if('abcde'.includes(item)) {
...
}