Skip to content
Advertisement

Can’t overwrite javascript array

I need to monitor 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.

let 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;
}

Advertisement

Answer

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;
}
Advertisement