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

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

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 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