For a straightforward logging system, I've devised a method of storing arrays as log entries within a single array. Here's how the code functions:
var myarr = new Array();
var secondarr = new Array(4,5,6);
myarr.push(secondarr);
secondarr.length=0;
secondarr.push(5,5,5);
myarr.push(secondarr);
secondarr.length=0;
//check
for(var i = 0; i < myarr.length; i++){
var str="";
for(var j = 0; j < myarr[i].length; j++)
str+=myarr[i][j]+" ";
console.log(str);
}
To streamline the logging process, I initialize a single array myarr
to store log entries. Data for logging is stored in secondarr
. To avoid creating a new array each time something is logged into myarr
, I attempted to reset secondarr
via secondarr.length=0
.
However, this action also clears out everything from
myarr</code due to references!</p>
<p>Is there a workaround to prevent the necessity of generating new arrays for each log entry? While using <code>secondarr.slice()
works, it leads to multiple arrays which I'm aiming to steer away from.