如何在数据数组中正确使用JavaScript indexOf

时间:2022-08-23 08:01:38

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.

我猜变量idx的值应该是1,因为它是数组集合中的第二个值。但结果是-1。

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

这是为什么呢?JavaScript日期类型有什么特别需要注意的地方吗?

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>

5 个解决方案

#1


36  

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

两个对象永远不会相等,除非你序列化它们。幸运的是,将Date序列化为整数非常容易。

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

#2


4  

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:

ECMAScript 6引入了数组#findIndex,它接受一个比较回调:

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

Browser support isn't great yet though.

浏览器支持还不是很好。

#3


2  

indexOf() won't work here... It's been well explained in the previous answers...

indexOf()不会在这里工作…在之前的答案中有很好的解释……

You'd be able to create your own lookup for the index. Here's a simple example comparing the dates using their .getTime() value...

您可以为索引创建自己的查找。这里有一个使用.getTime()值比较日期的简单示例…

(function() {

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

  var idx1 = -1;
  collection.forEach(function(item, index){
    if(d.getTime() == item.getTime())
      idx1 = index;
  });

  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>

#4


1  

Array.prototype.indexOfDate = function(date){
   for (var i = 0; i < this.length; i++){
      if (+this[i] === +date) return i;
   };
   return -1;
};

// then 
var idx1 = collection.indexOfDate(d);

#5


0  

This is because your "d" object is the different object. In other words:

这是因为“d”对象是不同的对象。换句话说:

var d = new Date(2014, 11, 24);

d === new Date(2014, 11, 24); // returns false

You can try this:

你可以试试这个:

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

var idx = collection.indexOf(d); // returns 1

#1


36  

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

两个对象永远不会相等,除非你序列化它们。幸运的是,将Date序列化为整数非常容易。

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

#2


4  

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:

ECMAScript 6引入了数组#findIndex,它接受一个比较回调:

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

Browser support isn't great yet though.

浏览器支持还不是很好。

#3


2  

indexOf() won't work here... It's been well explained in the previous answers...

indexOf()不会在这里工作…在之前的答案中有很好的解释……

You'd be able to create your own lookup for the index. Here's a simple example comparing the dates using their .getTime() value...

您可以为索引创建自己的查找。这里有一个使用.getTime()值比较日期的简单示例…

(function() {

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

  var idx1 = -1;
  collection.forEach(function(item, index){
    if(d.getTime() == item.getTime())
      idx1 = index;
  });

  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>

#4


1  

Array.prototype.indexOfDate = function(date){
   for (var i = 0; i < this.length; i++){
      if (+this[i] === +date) return i;
   };
   return -1;
};

// then 
var idx1 = collection.indexOfDate(d);

#5


0  

This is because your "d" object is the different object. In other words:

这是因为“d”对象是不同的对象。换句话说:

var d = new Date(2014, 11, 24);

d === new Date(2014, 11, 24); // returns false

You can try this:

你可以试试这个:

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

var idx = collection.indexOf(d); // returns 1