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

65
Views
How filter nested array of objects by date

I have an array like this:

const data =  [
  {
    id: '3499934913',
    user_data: {
      user_data_stays: [
        {
          value: '30.0',
          date: '2021-10-01T12:55:00.000Z',
        },
        {
          value: '30.0',
          date: '2021-11-01T12:55:00.000Z',
        },
        {
          value: '30.0',
          date: '2021-13-01T12:55:00.000Z',
        }
      ],
    }
  },
  {
    id: '43534535',
    user_data: {
      user_data_stays: [
        {
          value: '30.0',
          date: '2021-11-01T12:55:00.000Z',
        },
        {
          value: '30.0',
          date: '2021-12-01T12:55:00.000Z',
        },
        {
          value: '30.0',
          date: '2021-13-01T12:55:00.000Z',
        }
      ],
    }
  },
]

How can I keep the structure of the whole array but filter in user_data_stays only those objects which date is equal to 2021-10-01T12:55:00.000Z?

I tried using filter and some but I am not able to get to the nested element inside. I think the problem is the amount of nesting, and I can't deal with that. I would appreciate your help.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use forEach on the outer array and run filter method on inner array with the help of de-structuring, the object reference will update the outer object in-place.

const data = [
  {
    id: "3499934913",
    user_data: {
      user_data_stays: [
        { value: "30.0", date: "2021-10-01T12:55:00.000Z" },
        { value: "30.0", date: "2021-11-01T12:55:00.000Z" },
        { value: "30.0", date: "2021-13-01T12:55:00.000Z" },
      ],
    },
  },
  {
    id: "43534535",
    user_data: {
      user_data_stays: [
        { value: "30.0", date: "2021-11-01T12:55:00.000Z" },
        { value: "30.0", date: "2021-12-01T12:55:00.000Z" },
        { value: "30.0", date: "2021-13-01T12:55:00.000Z" },
      ],
    },
  },
];


data.forEach(({ user_data }) => {
  user_data.user_data_stays = user_data.user_data_stays
    .filter(({ date }) => date === '2021-10-01T12:55:00.000Z');
});

console.log(data);

about 4 years ago · Juan Pablo Isaza 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!