我有一个带有嵌套对象的对象,如下所示:
var SimpleWeapons = {
properties: "SimpleWeapons",
Club:{Name:"Club", Cost:"1sp", Damage:"1d4 bludgeoning", Weight:"2lb", Properties:"Light"},
Dagger:{Name:"Dagger" , Cost:" 2 gp" , Damage: "1d4 piercing", Weight:"1lb" , Properties:"Finesse, light, thrown (range 20/60)"},
Greatclub:{Name:"Greatclub" , Cost:"2sp" , Damage: "1d8 bludgeoning ", Weight:"10 lb" , Properties:"Two-handed"},
Handaxe:{Name:"Handaxe" , Cost:"5gp" , Damage: "1d6 slashing", Weight:"2lb" , Properties:"Light, thrown (range 20/60)"},
Javelin:{Name:"Javelin" , Cost:"5sp" , Damage: "1d6 piercing", Weight:"2lb" , Properties:"Thrown (range 30/120)"},
LightHammer:{Name:"Light Hammer" , Cost:"2gp" , Damage: "1d4 bludgeoning", Weight:"2lb" , Properties:"Light, thrown (range 20/60)"},
Mace:{Name:"Mace" , Cost:"5gp" , Damage: "1d6 bludgeoning", Weight:"4lb" , Properties:""},
Quarterstaff:{Name:"Quarterstaff" , Cost:"2sp" , Damage: "1d6 bludgeoning", Weight:"4lb" , Properties:"Versatile (1d8)"},
Sickle:{Name:"Sickle" , Cost:"1gp" , Damage: "1d4 slashing", Weight:"2lb" , Properties:"Light"},
Spear:{Name:"Spear" , Cost:"1gp" , Damage: "1d6 piercing", Weight:"3lb" , Properties:"Thrown (range 20/60), versatile (1d8)"}
}
我想随机返回嵌套对象属性之一(作为字符串),因此使用函数“Club”或“Dagger”。我在这个项目中以下列方式使用_.sample
并使用了更扁平的对象:_.sampleSize
var getDefaultEquipment = (chaClass) => {
if(chaClass === "Bard"){
var equipment = {};
equipment.equipment = (_.sampleSize(classes.Bard.equipment,1));
return equipment;}}
但我不确定如何深入挖掘,或者即使有可能?