Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly use JavaScript indexOf in a date array

Here is the code:

var collection = [new Date(2014, 11, 25), new Date(2014, 11, 24)];
var d=new Date(2014, 11, 24);

var idx= collection.indexOf(d);

I guess the variable idx should have a value of 1 since it is the second value in the array collection. But it turns out to be -1.

Why is that? Is there any special thing for the JavaScript Date type I need to pay attention?

Here is a snippet:

(function() {

  var collection = [new Date(2014, 11, 25), new Date(2014, 11, 24)];
  var d = new Date(2014, 11, 24);

  var idx1 = collection.indexOf(d);

  var intArray = [1, 3, 4, 5];
  var idx2 = intArray.indexOf(4);

  $('#btnTry1').on('click', function() {
    $('#result1').val(idx1);
  });

  $('#btnTry2').on('click', function() {
    $('#result2').val(idx2);
  });
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Index:
<input type="text" id="result1" value="">
<button id="btnTry1">Find index in a date array</button>
<br />Index:
<input type="text" id="result2" value="">
<button id="btnTry2">Find index in a regular array</button>
like image 813
Blaise Avatar asked Dec 12 '14 19:12

Blaise


People also ask

How to use indexOf () method in JavaScript array?

The JavaScript array indexOf () method is used to search the position of a particular element in a given array. This method is case-sensitive. The index position of first element in an array is always start with zero. If an element is not present in an array, it returns -1.

How do I find the index of an array element?

The indexOf method in JavaScript If you are working with arrays in JavaScript, particularly with large arrays, there may be a scenario to find only the specific array element to accomplish a certain task. The indexOf method, as the name shows, is used to search the index of an array element.

How do you search an array in JavaScript?

JavaScript Array indexOf() Method ... The indexOf() method searches the array for the specified item, and returns its position. The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array.

What is the use of indexOf in Java?

Definition and Usage The indexOf () method searches the array for the specified item, and returns its position. The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array. Returns -1 if the item is not found.


2 Answers

Two Objects will never be equal unless you serialise them. Lucky, Date is pretty easy to serialise as an integer.

var collection = [new Date(2014, 11, 25), new Date(2014, 11, 24)],
    d = new Date(2014, 11, 24),
    idx;

idx = collection.map(Number).indexOf(+d); // 1
//              ^^^^^^^^^^^^         ^ serialisation steps
like image 92
Paul S. Avatar answered Sep 28 '22 05:09

Paul S.


Two different objects are never equal to each other, even if they have the same properties / values. Here is a forward looking answer to the problem:

ECMAScript 6 introduces Array#findIndex which accepts a comparison callback:

var index = collection.findIndex(function(x) { 
    return x.valueOf() === d.valueOf(); 
});

Browser support isn't great yet though.

like image 8
Felix Kling Avatar answered Sep 28 '22 06:09

Felix Kling