javascript - How do I build an array in JS where some elements are conditional? -
i want build array may or may not contain elements, depending on user roles. can achieve @ "build time", or have first create array without conditional elements, , manually add them splice option? order matters!
this want achieve...:
var mylist = [ { obj1 }, { obj2 }, if userrolex==true { { obj3 }, } { obj4 }, ];
any thoughts?
you can build array elements needed:
var = 0; var mylist = []; mylist[i++] = obj1; mylist[i++] = obj2; if (userrolex) { mylist[i++] = obj3; } mylist[i++] = obj4;
you can skip indexes or provide non-numeric indexes so:
mylist[42] = obj5; mylist['foo'] = 'bar';
javascript's arrays can used maps. can "clear" entry setting undefined
.
Comments
Post a Comment