Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

493
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!