Jquery/javascript named index array with array association -
i want know if possible (and correct) construct named index array associated other arrays.
e.g.,
var signoptionset = []; signoptionset['basic'] = ['sign width', 'sign height', 'sign depth', 'base material', 'finish']; signoptionset['advanced'] = ['area', 'perimeter', 'lighting'];
how access 'sign height' in signoptionset['basic']?
i want know if possible...
yes, although way you're using it, wouldn't want array, you'd want object ({}
) (see below).
...(and correct)...
some argue using array , adding non-index properties not correct. there's nothing technically wrong it, provided understand things (such json.stringify
) won't see non-index properties.
how access 'sign height' in signoptionset['basic']?
it's second entry, index 1, of array you've assigned signoptionset['basic']
, so:
var x = signoptionset['basic'][1];
or
var x = signoptionset.basic[1];
using object, , using property name literals there's no need strings here, this:
var signoptionset = { basic: ['sign width', 'sign height', 'sign depth', 'base material', 'finish'], advanced: ['area', 'perimeter', 'lighting'] };
you can use strings, though, if prefer:
var signoptionset = { "basic": ['sign width', 'sign height', 'sign depth', 'base material', 'finish'], "advanced": ['area', 'perimeter', 'lighting'] };
both single , double quotes valid in javascript (single quotes wouldn't in json, isn't json, it's javascript):
var signoptionset = { 'basic': ['sign width', 'sign height', 'sign depth', 'base material', 'finish'], 'advanced': ['area', 'perimeter', 'lighting'] };
Comments
Post a Comment