Compare ISODate and two json ObjectId (JavaScript)

0

I would like to compare two jsons, but I have a problem with dates and objects. I do not know how I can convert them or compare them between two jsons to see if they are the same or not.

JSON 1:

    {
        "_id" : ObjectId("85f84214452251fec9ac18"),
        "tarea" : {
            "fechaInicio" : ISODate("2017-09-30T00:00:00.000Z"),
            "fechaFin" : ISODate("2017-09-50T00:00:00.000Z"),
            "descripcion" : "texto",
         }
    }

JSON 2:

    {
        "_id" : ObjectId("99f9992251fec9999"),
        "tarea" : {
            "fechaInicio" : ISODate("2017-08-22T00:00:00.000Z"),
            "fechaFin" : ISODate("2017-08-25T00:00:00.000Z"),
            "descripcion" : "texto",
         }
    }

This is a small example, as you see the _id and the dates are different, but I do not know how to convert them so that they can be compared ... How can I compare the ISODate attributes and the ObjectId?

    
asked by Norak 06.09.2017 в 12:03
source

1 answer

0

If you want to compare the whole objects you can do it by using the function JSON.stringify :

var a = JSON.stringify("{        \"_id\" : ObjectId(\"85f84214452251fec9ac18\"),        \"tarea\" : {            \"fechaInicio\" : ISODate(\"2017-09-30T00:00:00.000Z\"),            \"fechaFin\" : ISODate(\"2017-09-50T00:00:00.000Z\"),            \"descripcion\" : \"texto\",         }    }")
var b = JSON.stringify("{        \"_id\" : ObjectId(\"99f9992251fec9999\"),        \"tarea\" : {            \"fechaInicio\" : ISODate(\"2017-08-22T00:00:00.000Z\"),            \"fechaFin\" : ISODate(\"2017-08-25T00:00:00.000Z\"),            \"descripcion\" : \"texto\",         }    }")
a == b -> false



Another option would be to work with the JSON objects and compare their attributes:

a = {
    "_id" : 'ObjectId("85f84214452251fec9ac18")',
    "tarea" : {
        "fechaInicio" : 'ISODate("2017-09-30T00:00:00.000Z")',
        "fechaFin" : 'ISODate("2017-09-50T00:00:00.000Z")',
        "descripcion" : "texto",
     }
}
b = {
    "_id" : 'ObjectId("99f9992251fec9999")',
    "tarea" : {
        "fechaInicio" : 'ISODate("2017-08-22T00:00:00.000Z")',
        "fechaFin" : 'ISODate("2017-08-25T00:00:00.000Z")',
        "descripcion" : "texto",
     }
}

In this way you could directly compare the attributes one by one:

a._id == b._id -> false
a.tarea.fechaInicio == b.tarea.fechaInicio -> false
a.tarea.fechaFin == b.tarea.fechaFin -> false


Keep in mind that a JSON object always consists of a collection of name: value pairs (or a list), therefore ObjectId and ISODate are not attributes but values of _id and Startdate / Enddate.

    
answered by 06.09.2017 в 13:03