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

116
Visualizações
Filter an array of objects by key and nested value in JavaScript

I have an array of Objects relating to events. The events are organised by key/value where the Key is a DateTime of the beginning of the month, and the value is an Array or objects which have a specific DateTime within that month.

allEvents = [
    {
        key: "Dec 2021",
        value: [
            { id: 0, date: "Dec 06 2021" },
            { id: 1, date: "Dec 01 2021" },
        ],
    },
    {
        key: "Nov 2021",
        value: [
            { id: 0, date: "Nov 27 2021" },
            { id: 1, date: "Nov 23 2021" },
            { id: 2, date: "Nov 10 2021" },
        ],
    },
    {
        key: "Oct 2021",
        value: [
            { id: 0, date: "Oct 27 2021" },
            { id: 1, date: "Oct 23 2021" },
            { id: 2, date: "Oct 10 2021" },
        ],
    },
];

I need to filter these events by a given date range, eg Nov 15 2021 - Dec 04 2021

This should filter out the October object and also any events which fall within the given date Range while keeping the parent object. So the result would be:

filteredEvents = [
    {
        key: "Dec 2021",
        value: [
            { id: 1, date: "Dec 01 2021" },
        ],
    },
    {
        key: "Nov 2021",
        value: [
            { id: 0, date: "Nov 27 2021" },
            { id: 1, date: "Nov 23 2021" },
        ],
    },
];

So far, I've got the filtering done at the month level as below, but can't figure out how to handle the day level.

const dateRange = ["Nov 15 2021", "Dec 04 2021"];
const min = dateRange[0].getTime();
const max = dateRange[1].getTime();

const filteredEvents = allEvents.filter(
  eventGroup => new Date(eventGroup.key).getTime() >= min && new Date(eventGroup.key).getTime() <= max);

Ideally would like to just use ES6, but can use lodash.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

for one thing,

const min = dateRange[0].getTime();
const max = dateRange[1].getTime();

needs to be:

const min = new Date(dateRange[0]).getTime();
const max = new Date(dateRange[1]).getTime();

Then, after the initial loop, I would do a map in order to remove elements from value. The logic would be very similar to the filter you already have.

about 4 years ago · Juan Pablo Isaza Relatório

0

This looks a little unwieldy but it's pretty simple. First, map your array and filter out any value.date that do not fit in the range. Then simply filter out any parent elements that have a zero-length value array.

const filteredEvents = (from, to) => allEvents
  .map( g => 
     ({...g, 
       value: g.value.filter(f => 
         new Date(f.date).getTime() >= new Date(from).getTime() && 
         new Date(f.date).getTime() <= new Date(to).getTime())
      }))
  .filter(f => f.value.length>0);

let allEvents = [{
    key: "Dec 2021",
    value: [{
        id: 0,
        date: "Dec 06 2021"
      },
      {
        id: 1,
        date: "Dec 01 2021"
      },
    ],
  },
  {
    key: "Nov 2021",
    value: [{
        id: 0,
        date: "Nov 27 2021"
      },
      {
        id: 1,
        date: "Nov 23 2021"
      },
      {
        id: 2,
        date: "Nov 10 2021"
      },
    ],
  },
  {
    key: "Oct 2021",
    value: [{
        id: 0,
        date: "Oct 27 2021"
      },
      {
        id: 1,
        date: "Oct 23 2021"
      },
      {
        id: 2,
        date: "Oct 10 2021"
      },
    ],
  },
];


const filteredEvents = (from, to) => allEvents
  .map( g => ({...g, value: g.value.filter(f => new Date(f.date).getTime() >= new Date(from).getTime() && new Date(f.date).getTime() <= new Date(to).getTime())})).filter(f => f.value.length>0);

console.log(filteredEvents("Nov 15 2021", "Dec 04 2021"));

about 4 years ago · Juan Pablo Isaza Relatório

0

The values for each event have a full date. Use them to exclude dates outside the range, then exclude any events with no values.

const allEvents = [
    {
        key: "Dec 2021",
        value: [
            { id: 0, date: "Dec 06 2021" },
            { id: 1, date: "Dec 01 2021" },
        ],
    },
    {
        key: "Nov 2021",
        value: [
            { id: 0, date: "Nov 27 2021" },
            { id: 1, date: "Nov 23 2021" },
            { id: 2, date: "Nov 10 2021" },
        ],
    },
    {
        key: "Oct 2021",
        value: [
            { id: 0, date: "Oct 27 2021" },
            { id: 1, date: "Oct 23 2021" },
            { id: 2, date: "Oct 10 2021" },
        ],
    },
];

const dateRange = ["Nov 15 2021", "Dec 04 2021"];
const [min, max] = dateRange.map(d => new Date(d).getTime());

const filteredEvents = allEvents.map(({key,value}) => ({
    key,
    value:value.filter(({date:d}) => 
        new Date(d).getTime() >= min && new Date(d).getTime() <= max)
}))
.filter(({value:v}) => v.length);
  
console.log( filteredEvents );

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