I have an OOD system for my current webApp that every element has an id and is placed in the elements array of each page. the id of each new element is a continuous number regardless of the page that the elemenet is placed at, so for example if we have 25 elements in a project that has 7 pages, the ID of the new element no matter which page it’s placed will be 26.
So for example page one of a project that has 2 elements in it with IDs of 1 and 4 will be :
[undefined, proto.constructor, undefined, undefined, proto.constructor ]
this way refrencing an element would be very easy because all I need is the page number and ID of the element and I can call the element, for example pages[1].elements[1]. But the problem that I have with this method is that it results in excessive number of “undefined” elements in the final JSON which makes the JSON too big unnecessarily. Is there any way around that ?
Use a JS object instead of an array. It can still have numeric-looking keys (they’re actually strings, but that’s true for arrays, too) and has the same constant-time lookup benefits.
So, instead of code like this…
…instead write code like so:
The JSON output will be terse (though not necessarily sorted):