Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

494
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda