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
Related
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Why does this index of a two dimensional array return -1 [duplicate]
(2 answers)
Closed 3 years ago.
I have simple nested array, like:
var arr = [[75.0], [65.0]] ;
and when I do:
arr.indexOf( [75.0] );
I expect 0, but I get -1, what gives?
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf :
indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).
There is the problem. In your example, you have an array of arrays. Comparing with === operator means that for it to evaluate to true, it has to be the same array object. Clearly it is a different object so it is not found from the array.
You need to use Array.find() instead where you can provide the testing function. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
[] is not identical to [] [duplicate]
(4 answers)
Closed 4 years ago.
In my current project I am taking a request parameter with a value of either array or a string. But if I get an array it would be an empty array. So what I did is I checked the type first and then I worked with the value. But then I did something like this
const reqParam = []
if (reqParam === []) {
console.log('empty array')
} else {
console.log('string')
}
But reqParam despite of being an empty array is giving me false while comparing with []. Why it is behaving like this? Thanks in advance.
Your code creates two different arrays, that's why the comparison is returning false. === will only compare their references and not their content.
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Check if an element is present in an array [duplicate]
(9 answers)
Closed 4 years ago.
I recently starting learning JS so a beginner level answer would be great! Thanks.
function contains(arr, item) {
// check to see if item is inside of arr
// return true if it is, otherwise return false
}
What about something like this?
const arr = [1,2,3];
const el = 2;
console.log(arr.includes(el)) // true
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:
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";
}