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.
JavaScript
x
24
24
1
let playlists = [];
2
results = simpleMysqlQuery();
3
setinterval{
4
update(playlists, results);
5
}
6
7
function update(playlists, results){
8
if(playlists.length != results.length){
9
playlists = reWritePlaylists(results, playlists);
10
}
11
}
12
13
function reWritePlaylists(results, playlists){
14
results.forEach(function(item, i, arr){
15
playlists[i] = new Object();
16
playlists[i]['id'] = results[i]['id'];
17
playlists[i]['name'] = results[i]['name'];
18
});
19
if(playlists.length > results.length){
20
playlists = playlists.slice(0, results.length);
21
}
22
return playlists;
23
}
24
Advertisement
Answer
I found de wae!
JavaScript
1
10
10
1
function reWritePlaylists(results, playlists){
2
playlists.splice(0, playlists.length);
3
results.forEach(function(item, i, arr){
4
playlists[i] = new Object();
5
playlists[i]['id'] = results[i]['id'];
6
playlists[i]['name'] = results[i]['name'];
7
});
8
return playlists;
9
}
10