I am creating a JSON array exactly as follows (bar using mathrandom).
For example purposes:
var numbers = [];
var jsonString = "";
function EveryOneSec() {
numbers.push(Math.random());
jsonString = JSON.stringify({'numbers': numbers});
setTimeout(EveryOneSec, 1000);
}
When I create the JSON string it will obviously just keep getting bigger.
Is there a way that I can only have the 10 most recently added into the array?
Add the following code to the top of EveryOneSec
if (numbers.length == 10) {
numbers.shift();
}
You want to use push and shift to ensure you always have the recent 10.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With