Filter Nested Array from NodeJS

I have a nodejs Get request which returns the following array:

const array = [{
  name: 'Alex',
  age: 10,
  details: [{
    day: 'Monday',
    asst: 'Leo',
    subDetails: [{ color: 'green', qty: 85 }, { color: 'orange', qty: 35 }]
  },
  {
    day: 'Tuesday',
    asst: 'Shaun',
    subDetails: [{ color: 'brown', qty: 15 }, { color: 'indigo', qty: 35 }]
  },
  {
    day: 'Wednesday',
    asst: 'Julian',
    subDetails: [{ color: 'pink', qty: 25 }, { color: 'blue', qty: 15 }]
  },
  {
    day: 'Thursday',
    asst: 'Luis',
    subDetails: [{ color: 'peach', qty: 5 }, { color: 'black', qty: 15 }]
  },
  ]
}]

I want to further filter the result from the nodejs before it will be sent to the frontend.
I want to get the array and nested array on Monday.
Here is my desired array:

const desiredArray = [{
  name: 'Alex',
  age: 10,
  details: [{
    day: 'Monday',
    asst: 'Leo',
    subDetails: [{ color: 'green', qty: 85 }, { color: 'orange', qty: 35 }]
  }]
}]

This is my NodeJS get query:

router.get('/getByDay/:id/:day', async (req, res) => {
  try {
        const array = await getDay.find({_id: req.params.id  });

        res.json({ success: true, array })
  } catch (err) { res.json({ success: false, message: 'An error occured' }); }
});

81 thoughts on “Filter Nested Array from NodeJS”

Leave a Comment