Index of array? [duplicate] - javascript

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

Related

indexOf not working correctly in javaScript [duplicate]

This question already has answers here:
Difference Between indexOf and findIndex function of array
(9 answers)
Closed 4 months ago.
my code is :
[{a:1},{b:2},{c:3}].indexOf(obj=>{return obj.a ==1})
I expect return 0 but result is -1
what is the problem?
The main difference are the parameters of these functions:
Array.prototype.indexOf() expects a value as first parameter. This
makes it a good choice to find the index in arrays of primitive types
(like string, number, or boolean).
Array.prototype.findIndex() expects a callback as first parameter.
Use this if you need the index in arrays with non-primitive types
(e.g. objects) or your find condition is more complex than just a
value.
This question is similar to this and the answer can also be found there.

In operator returns false [duplicate]

This question already has answers here:
javascript string in list returns false
(3 answers)
Closed 3 years ago.
Why this statement returns false? It's really weird
console.log("100038916831294" in ["100003748210938", "100038916831294"]);
The in operator tells you whether a value exists as a property name in an object. The property names of your array are "0" and "1".
You can use one of the Array methods to check if a value is in the array, like .indexOf() or .includes():
console.log(["100003748210938", "100038916831294"].includes("100038916831294"));
The in operator in JavaScript compares indexes or property names in arrays instead of the value itself.
For example, if we write console.log(0 in ["abc","pqr"]); it will print true. However, if we use the value, like in console.log("abc" in ["abc","pqr"]); it will print false.
You can further read about it on https://www.w3schools.com/jsref/jsref_operators.asp.

Comparing two empty array not working in javascript [duplicate]

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.

Math.min give NaN [duplicate]

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

In javascript, [] === [] and [] == [] both returns false [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 7 years ago.
why is that? I assumed it's some implicit type conversion at first, but [] == [] is also false.
Arrays in javascript are Objects. Objects are compared by identity. So no two objects created by different literals (or by other means) are going to be equal (either strictly (===) or loosely (==)).

Categories

Resources