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

157
Views
Can't overwrite javascript array

I want to track changes of "results". If the results.length increases, the array will be overwritten and saved. If the length decreases, then the array will be overwritten, but the new value won't save.

playlists = [];
results = simpleMysqlQuery();
setinterval{
    update(playlists, results);
}

function update(playlists, results){
    if(playlists.length != results.length){
        playlists = reWritePlaylists(results, playlists);
    }
}

function reWritePlaylists(results, playlists){
   results.forEach(function(item, i, arr){
      playlists[i] = new Object();
      playlists[i]['id'] = results[i]['id'];
      playlists[i]['name'] = results[i]['name'];
   });
   if(playlists.length > results.length){
      playlists = playlists.slice(0, results.length);
   }
   return playlists;
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

When you use slice and create a new array, the newly created array is no longer a reference to the original array, so the playlists parameter in update in no longer referring to the same array instance as the playlists variable outside the function. Try this:

playlists = [];
results = simpleMysqlQuery();
setinterval{
    playlists = update(playlists, results);
}

function update(playlists, results){
    if(playlists.length != results.length){
        playlists = reWritePlaylists(results, playlists);
    }
    return playlists
}

function reWritePlaylists(results, playlists){
   results.forEach(function(item, i, arr){
      playlists[i] = new Object();
      playlists[i]['id'] = results[i]['id'];
      playlists[i]['name'] = results[i]['name'];
   });
   if(playlists.length > results.length){
      playlists = playlists.slice(0, results.length);
   }
   return playlists;
}
about 4 years ago · Juan Pablo Isaza Report

0

Please update reWritePlaylists function like below:

function reWritePlaylists(results, playlists){
   playlists.splice(0, results.length);
   results.forEach(function(item, i, arr){
      playlists[i] = new Object();
      playlists[i]['id'] = results[i]['id'];
      playlists[i]['name'] = results[i]['name'];
   });
   return playlists;
}

about 4 years ago · Juan Pablo Isaza Report

0

I found de wae!

function reWritePlaylists(results, playlists){
   playlists.splice(0, playlists.length);
   results.forEach(function(item, i, arr){
      playlists[i] = new Object();
      playlists[i]['id'] = results[i]['id'];
      playlists[i]['name'] = results[i]['name'];
   });
   return playlists;
}
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!