Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!