Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

505
Vistas
How to determine intersect objects in json array with javascript?

I have json data like this:

[{
        "Id": 1,
        "Name": "Test 1",
        "Time": {
            "start": "22:00",
            "end": "22:30",
            "duration": 30
        }
    }, {
        "Id": 2,
        "Name": "Test 2",
        "Time": {
            "start": "22:05",
            "end": "22:35",
            "duration": 30
        }
    }, {
        "Id": 3,
        "Name": "Test 3",
        "Time": {
            "start": "23:25",
            "end": "23:40",
            "duration": 15
        }
    },{
        "Id": 4,
        "Name": "Test 4",
        "Time": {
            "start": "22:15",
            "end": "22:50",
            "duration": 15
        }
    }
]

And i want get result like this, determine which intersect by Time:

  [
    ["Test 1", "Test 2", "Test 4"],
    ["Test 3"]
]

I have solution, but it's too complicated and i use 4 loops inside each other

If there is an easier solution, please suggest, thanks!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try aggregation, I am not sure this will work exact as per your requirement,

  • $addFields add new field startHour, that splits string hour and minute from string, using $split, and $arrayElemAt return first element means hour
  • $group by startHour and make array of Name
db.collection.aggregate([
  {
    $addFields: {
      startHour: {
        $arrayElemAt: [{ $split: ["$Time.start", ":"] }, 0]
      }
    }
  },
  {
    $group: {
      _id: "$startHour",
      Name: { $push: "$Name" }
    }
  }
])

Playground

Result:

[
  {
    "Name": ["Test 1","Test 2","Test 4"],
    "_id": "22"
  },
  {
    "Name": ["Test 3"],
    "_id": "23"
  }
]
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda