In SQL Server, I could say:
WHERE X IN(1,2)
How would you rewrite the following in JavaScript:
if (X==1 || X==2) {}
To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.
function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = 0; j < myArray. length; j++) { if (i != j) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } } return false; // means there are no duplicate values. }
Use indexOf
to see if x
is in an array.
if([1,2].indexOf(x) !== -1)
Using Array .includes
method.
if ([1, 2].includes(x)) { // array has x }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With