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

126
Vistas
Make a new array with javascript

Need help to make new array, when I have two array

First one:

var start=[ "11/10/2021", "05/10/2021", "16/10/2021", "03/10/2021" ]

The second:

var end=[ "10/10/2021", "04/10/2021", "15/10/2021", "01/10/2021" ]

And I make a new array with code, but get an error:

var start= [];
var end= [];
var daterange =[];
var daterange = JSON.stringify(response);
var textdiv=JSON.parse(daterange );
for (var i = 0; i < textdiv.length; i++) {
     sart.push(textdiv[i].a);
     end.push(textdiv[i].b);
     daterange.push('start'=>moment(textdiv[i].a),'end'=>moment(textdiv[i].b));
} 

So the output is as follows:

var daterange =[{
    'start': moment('2021-10-10'),
    'end': moment('2021-10-15')
  },
  {
    'start': moment('2021-10-25'),
    'end': moment('2021-10-30')
  }
];
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I took whatever is coming in textdiv and store it in proper key,value pair as required in daterange array via creating an object. Moment only converts string to date in given format.

    var start= []; // if you already have start, you can not reinitialize it, I think you are creating a fresh array here for later use.
    var end= [];
    var daterange =[];
    var daterange = JSON.stringify(response);
    var textdiv=JSON.parse(daterange );
console.log(textdiv, daterange); // please check what you are actually getting after parsing to understand your error better
    for (var i = 0; i < textdiv.length; i++) {
         let a = textdiv[i].a; // there is a typo here
         let b = textdiv[i].b;
         daterange.push({'start' :moment(a).format('YYYY-MM-DD'),'end':moment(b).format('YYYY-MM-DD')});// you need to store them as object if you want to push them together and use format function to properly insert it as date.
    } 
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