Javascript best practice to handle string array base 1 -
i need print string of array, receive integer back-end, can 1, 2, 3 or 4.
which best way print correct string array? i've thought 2 possible solutions.
var risklevelmap = [ "", "low", "mediumlow", "medium", "high" ]; console.log(risklevelmap[ integervalue ]);
or
var risklevelmap = [ "low", "mediumlow", "medium", "high" ]; console.log(risklevelmap[ integervalue-1 ]);
for sake of clarity, go second option.
as awkward may seem, additional empty string can troublesome.
[ "", "low", "mediumlow", "medium", "high" ];
technically takes (marginally) more space. so, memory optimization standpoint, technically less desirable. isn't convention have used elements start @ 1. confuse when writing code down rode.
granted, [ "low", "mediumlow", "medium", "high" ];
has disadvantages. additional calculation required return desired value. so, calculation optimization standpoint, technically less desirable.
thankfully, both marginally better or worse other.
because first option "breaks convention", suggest second option. more intuitive, , isn't inserting useless padding elements.
Comments
Post a Comment