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

118
Views
clear array while inside loop javascript

I have this loop:

for (let i = 0; i < rows.length; i++) {
            let sessionString = rows[i][weekday];
            //finalSessionString is the variable for splitting the string into 4 parts
            const finalSessionString = sessionString.split('|');
            //finalSessionString0 is the string that holds the value of (ex) 01 11 * * 1
            const finalSessionString0 = finalSessionString[0]
            //finalSessionStringPhoneNumber gets phone number from finalSessionString
            const finalSessionStringPhoneNumber = finalSessionString[1]
            //finalSessionStringName is getting the name of the person training
            const finalSessionStringName = finalSessionString[2]
            //finalSessionStringTrainerName is getting the name of the trainer
            const finalSessionStringTrainerName = finalSessionString[3]
            console.log("kk: " + finalSessionString[1])
            console.log(finalSessionString0)
            console.log(finalSessionStringPhoneNumber)
            console.log(finalSessionStringName)
            console.log(finalSessionStringTrainerName)
            var textJob = new cronJob( finalSessionString0, function(){
                client.messages.create( { to: finalSessionStringPhoneNumber, from:'12055578708', body:'hello ' + finalSessionStringName + ', Your training session just finished with ' +  finalSessionStringTrainerName}, function( err, data ) {
                    if (err) {
                        console.log("err: " + err)
                    } 
                    console.log(data)
                });
            },
            null, true)
            finalSessionString.length = 0
        }

finalSessionString is an array, that produces a response like so:

 [
  '05 10 * * 2',
  '1234567890',
  'Gianluca A',
  'gianluca@mmm.com'
]

so after i split up the array into multiple parts, I want it to clear the array length, because I am also running a cronjob which runs every 45 seconds, so every 45 seconds it is inserting a new value into the array! So after every time it inserts a new record into the array, I need it to clear it first. that's what I tried to do here

finalSessionString.length = 0

but it isn't working, because i got 5 text messages after 5 rounds of the cronjob running. So how can i fix this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you just want to clear the aray before the Cron job just do

finalSessionString = [ ];
finalSessionString.push(value);
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!