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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27450867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:30:44  来源:igfitidea点击:

How to correctly use JavaScript indexOf in a date array

javascript

提问by Blaise

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 idxshould have a value of 1since it is the second value in the array collection. But it turns out to be -1.

我猜这个变量idx应该有一个值,1因为它是数组中的第二个值collection。但事实证明是-1

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

这是为什么?JavaScriptDate类型有什么特别需要注意的地方吗?

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>

回答by Paul S.

Two Objectswill never be equal unless you serialise them. Lucky, Dateis 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

回答by Felix Kling

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#findIndexwhich accepts a comparison callback:

ECMAScript 6 引入了Array#findIndex它接受比较回调:

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

Browser support isn't great yet though.

浏览器支持还不是很好。

回答by LcSalazar

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>

回答by Freez

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);

回答by Seva Arkhangelskiy

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