Best way to remove a row off an object based off a certain condition?
Down below I made a function to remove any row where the difference of the amount within latest
row and any other row is greater than 300.
bank_data = [{"arrival_time":"12:00","amount":"900","id":"BS"},
{"arrival_time" :"12:00","amount":"300","id":"BD"},
{"arrival_time" :"01:00","amount":"400","id":"SW"},
{"arrival_time" :"03:00","amount":"800","id":"OS"},
{"arrival_time" :"03:00","amount":"500","id":"SF"},
{"arrival_time" :"04:00","amount":"600","id":"SJ"},
{"arrival_time" :"04:00","amount":"600","id":"FS"},
{"arrival_time" :"06:00","amount":"900","id":"JC"}];
function update_list(obj){
if(obj.length > 0) {
latest = obj.length -1;
for(var i = 0; i < obj.length; i++){
if((obj[latest].amount - obj[i].amount) > 300){
obj.splice(i, 1);//remove the ith row of the object
}
}
}
return obj;
}
Goal Result
bank_data = [{"arrival_time":"12:00","amount":"900","id":"BA"},
{"arrival_time" :"03:00","amount":"800","id":"OS"},
{"arrival_time" :"04:00","amount":"600","id":"SJ"},
{"arrival_time" :"04:00","amount":"600","id":"FS"},
{"arrival_time" :"06:00","amount":"900","id":"JC"}];