This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 7 years ago.
How can I simplfy the following text inside the if statement in Javascript using "indexof"?
if (a === 1 || a === 2 || a === 3) {
return "correct";
};
I am guessing an array needs to be made for 1,2, and 3, but am unsure of how to us instanceof after that
*edited to say indexOf instead of instanceOf
The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.
In your case, instanceof wont help you. You can use indexOf() with array as follow.
var arr = [1, 2, 3];
// Check if a is present in the arr array
if (arr.indexOf(a) > -1) {
return "correct";
}
Related
This question already has answers here:
Changing the behaviour of the typeof operator in Javascript
(4 answers)
Closed 3 years ago.
If a is an instance of Array. We know typeof a will returns 'object', but I want to make it return 'array', can I do this?
If I can then how do I do it
If not then why not?
No, you can't - an array is an object, plain and simple:
const aRealArray = [1, 2, 3];
console.log(typeof aRealArray);
You can check if something is an array, however, by using Array.isArray or instanceof
const aRealArray = [1, 2, 3];
console.log(Array.isArray(aRealArray));
console.log(aRealArray instanceof Array);
console.log(Object.prototype.toString.call(aRealArray));
Note that Array.isArray is the more reliable version (and as pointed out by #MattBrowne in the comments below, it works across iframes). instanceof simply checks if Array.prototype is on a prototype chain.
Try using Array's "isArray" method, which returns a boolean:
Array.isArray(a)
This question already has answers here:
Why does Math.min([1,2]) return NaN?
(3 answers)
Closed 5 years ago.
I am creating array using $.map as below
var correctScores = $('[id^="dvAns_"] input[type="text"]').map(function () {
return $(this).val().trim() == "" ? 0 : parseFloat($(this).val());
}).get();
it gives me array as [5, 0]
when I am writing Math.min(correctScores) it returns NaN instead of 0.
Why so?
You will have to use apply to be able to use Math.min() in connection with an array. In its native form it expects individual arguments.
See here: Math.min.apply(0, array) - why?
It needs to be out of array, like this;
Math.min(correctScores[0],correctScores[1]); // 0
This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Closed 6 years ago.
quick question:
why does this return false? Just curious.
var myArray = [];
var myArray1 = new Array();
console.log(myArray === myArray1)
Two distinct objects are never === to one another (nor are they ==, for that matter). Object equality means that the two objects are really just one object; that is, that both sides of the === operator are references to the exact same object.
So, this will give you true:
var a = [], b = a;
console.log(a === b);
Every time you do
new Array()
or
= []
A new instance of the Array constructor is assigned to the variable. So when you do a equality check they aint same. Just as same as when two instance of a class aint same.
This question already has answers here:
Check if an element is present in an array [duplicate]
(9 answers)
Closed 6 years ago.
let array = [1234, 1233, 1232];
console.log(_.some(array, 1234));
It returns false. Do you know why?
As per the documentation of _.some() method, second argument should be a predicate function
console.log(_.some(array, function(v){ return v === 1234}));
In this particular case you can simply use native javascript Array#indexOf method.
console.log(array.indexOf(1234) > -1);
Also there is native JavaScript Array#some method.
console.log(array.some(function(v){ return v === 1234}));
with ES6 arrow function
console.log(array.some(v => v == 1234))
With UNDERSCORE.JS you can simply use,
console.log(_.indexOf(array, 1234) >= 0)
Document for more details:
http://underscorejs.org/#indexOf
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 7 years ago.
I have two identical array and I want to see if it equals and return true
var a = [1,2];
var b = [1,2];
if (a===b) {
return true
}
these two array are obviously identical but I am getting not equal. Can some explain why and if there is an easy way to compare the two?
Problem is you are creating two different arrays, and === check whether both a and b have same reference. Hence your condition fails. There is no built-in code to compare array, however there are libraries for the same. But you simply write a function to compare the arrays by looping.
1. Don't ever use == operator
It doesn't do what you think and it's quite close to be totally useless (for example "1" == [[1]]). Prefer === instead. If the type is the same for both sides == and === do the same, but if they're not == does crazy conversions you will regret.
2. === for arrays checks identity
I.e. it will return true only if the two sides are the very same object, not an object with the same content (whatever 'same' is meant to be).
If you want to check the content you should first decide how to compare elements... for example
my_eqtest([1, [2, 3]], [1, [2, 3]])
should return true or false?
x = [1, 2]
y = [1, 2]
y.myextramember = "foo"
my_eqtest(x, y) // should be true or false?
You should describe (document) what you mean for equality if it's not object identity, otherwise who reads the code will not understand why something is not working (including yourself in a few weeks).
Easiest way would be use a utility library like lodash _.difference
try this:
function arraysEqual(arr1, arr2) {
if(arr1.length !== arr2.length)
return false;
for(var i = arr1.length; i--;) {
if(arr1[i] !== arr2[i])
return false;
}
return true;
}