Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
Sort nested object by date in javascript

How can i sort the data by date and find maximum value count between consecutive dates ? Example data:

const data = {
    'addias':{
        '2022-1-10': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2022-1-09': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2021-12-29': ['a'],
        '2021-12-28': ['y', 'u', 'v', 'd', 'f', 'f', 'g'],
        '2021-1-08': ['x', 't', 's', 'd', 'e', 'f', 'g'],
    },
    'nike':{
        '2022-1-20': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2022-1-15': ['a', 'b', 'c'],
        '2022-1-14': ['a', 'b'],
        '2022-1-10': ['y', 'b', 'x', 'z', 'e', 'f', 'g'],
    }
}

result be like

const result = {
     'addias':{
        'firstDate': '2022-1-09',
        'secondDate':'2022-1-10',
        'total':14 ,
    },
    'nike':{
        'firstDate': '2022-1-14',
        'secondDate':'2022-1-15',
        'total':5,
    }
}

Attempt:

Object.entries(data).forEach((item) => {
   const [ brand, dates ] = item; /** {'2022-1-10': array } */
   let sorted = Object.entries(dates).sort((a,b) => new Date(a[0]) - new Date(b[0]))
   /** find value count for each sorted date */
   
   /** find if the dates are consecutive */

   /** build new object with result property */ 

})

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think finding out the date pairs before counting would be more efficient, I think this should solve the problem:

const data = {
  'addias': {
    '2022-1-10': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    '2022-1-09': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    '2021-12-29': ['a'],
    '2021-12-28': ['y', 'u', 'v', 'd', 'f', 'f', 'g'],
    '2021-1-08': ['x', 't', 's', 'd', 'e', 'f', 'g'],
  },
  'nike': {
    '2022-1-20': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
    '2022-1-15': ['a', 'b', 'c'],
    '2022-1-14': ['a', 'b'],
    '2022-1-10': ['y', 'b', 'x', 'z', 'e', 'f', 'g'],
  }
};

function maxValueBetweenConsecutiveDates(input) {
  const result = {};
  for (const brand in input) {
    const dates = Object
      .keys(input[brand])
      .map(e => ({
        date: new Date(e),
        original: e
      }))
      .sort((a, b) => a.date - b.date);

    const pairs = [];

    for (let i = 1; i < dates.length; i++) {
      if (dates[i].date - dates[i - 1].date === 86400000) {
        pairs.push([dates[i - 1], dates[i]]);
      }
    }

    const quantities = pairs.map(pair => ({
      firstDate: pair[0].original,
      secondDate: pair[1].original,
      total: input[brand][pair[0].original].length + input[brand][pair[1].original].length
    }));
    const max = Math.max(...quantities.map(e => e.total));
    const quantity = quantities.find(e => e.total === max);

    result[brand] = quantity;
  }

  return result;
}

console.log(maxValueBetweenConsecutiveDates(data));

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could do it like this, altough you might want to write a custom sorting function

const data = {
    'addias':{
        '2022-1-10': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2022-1-09': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2021-12-29': ['a'],
        '2021-12-28': ['y', 'u', 'v', 'd', 'f', 'f', 'g'],
        '2021-1-08': ['x', 't', 's', 'd', 'e', 'f', 'g'],
    },
    'nike':{
        '2022-1-20': ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
        '2022-1-15': ['a', 'b', 'c'],
        '2022-1-14': ['a', 'b'],
        '2022-1-10': ['y', 'b', 'x', 'z', 'e', 'f', 'g'],
    }
}

const result = {};

Object.keys(data).forEach(sneaker => {
  const dates = Object.keys(data[sneaker]);
  dates.sort();
  result[sneaker] = {
    firstDate: dates[0],
    secondDate: dates[1],
    total: dates.length
  };
});

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda