Is it possible to find a Date value within an array?

3

Good afternoon, my query is if I have a value type Date and an array where its values are also type Date : is it possible to perform a search to see if there are matches? .

I'm doing the following:

// time = Sun Dec 13 2015 21:00:00 GMT-0300 (Hora est. Sudamérica Pacífico)

var busqueda = _.findWhere(this.events,{startsAt:moment(time).toDate()});
return !!busqueda;

The array contains:

0: Object
$id: 0
$priority: null
endsAt: Sun Dec 13 2015 21:15:00 GMT-0300 (Hora est. Sudamérica Pacífico)
startsAt: Sun Dec 13 2015 21:00:00 GMT-0300 (Hora est. Sudamérica Pacífico)
title: "titulo: 14.12.2015 00:00 - 14.12.2015 00:15"
type: "info"
__proto__: Object

So far this only returns undefined .

    
asked by Luis Ruiz Figueroa 18.12.2015 в 19:33
source

2 answers

5

Comparing dates in any language is usually a difficult task, for all the variants that exist and the methods that each language provides. In JavaScript an effective way to compare the equality of two dates is with the method getTime , you can easily implement a method to verify if a date is contained in an array:

function existeFecha(array, fecha) {
  return array.some(function(f){
    return f.getTime() === fecha.getTime();     
  });
}

With Underscore.js

function existeFecha(array, fecha) {
  return _.some(array, function(f){
    return f.getTime() === fecha.getTime();     
  });
}

The some method of an array returns a Boolean value that indicates if an element meets the condition specified in the callback, here we use the getTime method to verify if two dates are equal.

Here is a demo on JSBin

Here a post more detailed

    
answered by 18.12.2015 / 19:53
source
1

Taking into account that we have an arrangement with 3 objects Date inside and the variable of the date to be found:

var aEncontrar = new Date('2015/05/05'),
    arreglo = [
        new Date('2015/05/05'),
        new Date('2015/12/24'),
        new Date('2015/09/15')
    ];

Use of libraries

You can use a library like Underscore.js to give you tools like that for data filtering. Example:

Solution 1

  

_. find (list, predicate, [context]) : Go through each value in the array, returning the first match to pass the function (predicate), or undefined if the value does not pass the test.

var fechaEncontrada = _.find(arreglo, function (fecha) {
    return fecha.getTime() === aEncontrar.getTime();
});

Solution 2

  

_. some (list, [predicate], [context]) : Returns true if the value in the list passes the predicate test (predicate). The search stops if anyone passes the test.

_.some(arreglo, function (fecha) {
    return fecha.getTime() === aEncontrar.getTime();
});

There is another similar way but it does not provide the same result as the previous ones and it is with the use of jQuery . It consists of the following: you provide an array to iterate and a callback that establishes whether or not the value passes the scheduled test so that all those objects that match will be returned in an array.

  

jQuery.grep (array, function [ invert]) : Find the elements in an array that satisfy a filter function (return true ). The original arrangement is not affected.

Documentation at: jQuery.grep ()

    
answered by 22.12.2015 в 02:41