我在调用对象内的数组时遇到问题。
我有一个数组(在对象内)称为:items
。
而且我还有其他数组,如“ name_of_the_item_color_names
”和“ name_of_the_item_color_codes
”(只是为了清楚起见,name_of_the_item 是一个占位符,它是数组中的值之一items
)
无论如何,我想知道是否有办法访问它们,使用其中的一个值items
和除了_color_names
或_color_codes
。
顺便说一句,我知道有一种方法,[additions_colorizing.items[i]][total]
但我想知道我是否可以添加额外的部分(再次,_color_names
或_color_codes
)
这是部分代码的示例:
var additions_colorizing = {
items: ['',
'long_hair',
'fancy_hair',
'boys_hair',
'cadence_hair',
'mask',
'viking_helmet',
'hard_hat',
'nose',
'mask',
'snowtuber',
'normal_headphones',
'classic_headphones',
'party_headphones',
'cpi_party_hat',
'cp_party_hat',
'fairy_wings'
],
mask_color_names: ['',
'yellow',
'orange',
'peach',
'red',
'mint',
'green',
'olive_green',
'dark_green',
'blue',
'dark_blue',
'azure',
'azure2',
'light_pink',
'pink',
'purple',
'dark_purple',
'white',
'grey',
'black',
'brown'
],
mask_color_codes: ['',
'#FFF000',
'#FF8400',
'#FF894E',
'#EB0505',
'#00FF59',
'#4DCB09',
'#659B20',
'#19750D',
'#0486C8',
'#203D93',
'#53C5FF',
'#1BB3AE',
'#F862D0',
'#FD009B',
'#CC10E8',
'#680F93',
'#FFFFFF',
'#797979',
'#000000',
'#BA6C08'
]}
//there's more... but I deleted 90% of it ^^
你可以像这样访问你想要的变量:
var item = additions_colorizing.items[5]; //This will get the 'mask' value
var item_color_names = additions_colorizing[item + '_color_names']; //This gets the 'mask_color_names' array
var item_color_codes = additions_colorizing[item + '_color_codes']; //This gets the 'mask_color_codes' array
或者,additions_colorizing.items
您也可以使用继承/对象结构,而不是使用如此多的数组(每个项目 2 个),其中的每个项目additions_colorizing.items
都是具有项目值的对象(如果您想显示一个不错的字符串) ),一个包含该项目颜色名称的数组,以及另一个包含该项目颜色代码的数组。
我希望这可以帮助你。
祝你好运。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句