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

172
Visualizações
JSON array of objects with properties “from” and “to” and group it by time period

In case if dates in the array are intersect then function should combine them in one period, else periods should be separated

var dates = [
{
"from":"03/01/2021",
"to":"03/05/2021"
  },
  {
"from":"03/08/2021",
"to":"03/10/2021"
  },
  {
"from":"03/07/2021",
"to":"03/20/2021"
  }

   ];
var item = dates;
var index = 0;
var out = document.querySelector('#test');
for(var i = 0; i < item.length; i++){
    index++;
    var from_i = Date.parse(item[i].from);
    var to_i = Date.parse(item[i].to);
    var from_index = Date.parse(item[index].from);
    var to_index = Date.parse(item[index].to);
    if(from_index >= from_i && from_index <= to_i){
        let updated_from = new Date(from_index);
        let update_to = new Date(to_index);
        console.log(updated_from);
        out.innerHTML += updated_from.toDateString() + update_to.toDateString() + "<br/>";
    }
}
<div id="test"> </div>

Case of incoming jSON: [ { "from":"03/01/2021", "to":"03/06/2021" }, { "from":"03/10/2021", "to":"03/15/2021" }, { "from":"03/20/2021", "to":"03/25/2021" } ]

Output: Mar 1-5, 7-20

Can some help? Learning JS…little bit hard for me(

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

0

you can do something like this

const data = [ { "from":"03/01/2021", "to":"03/06/2021" }, { "from":"03/10/2021", "to":"03/15/2021" }, { "from":"03/20/2021", "to":"03/25/2021" } ]

const monthToString = month => {
  const months = {
    '01': 'Jan',
    '02': 'Feb',
    '03': 'Mar',
    '04': 'Apr',
    '05': 'May',
    '06': 'Jun',
    '07': 'Jul',
    '08': 'Aug',
    '09': 'Sep',
    '10': 'Oct',
    '11': 'Nov',
    '12': 'Dec',
    
  }
  return months[month] || month
}


const result = Object.entries(data.reduce((res, {from, to}) => {
  const [monthFrom, day, year] = from.split('/')
  const [m, dayTo, y] = to.split('/')
  
  const month = monthToString(m)
  
  return {
   ...res,
   [month]: [...(res[month] || []), [day, dayTo]]
  }
  
}, {})).map(([month, days]) => `${month} ${days.map(([from, to]) => `${parseInt(from)}-${parseInt(to)}`).join(', ')}`).join('\n')

console.log(result)

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