0

我有一个带有嵌套对象的对象,如下所示:

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;}}

但我不确定如何深入挖掘,或者即使有可能?

4

2 回答 2

2

我知道已经有一个公认的答案,但我还想展示如何使用该_.chain()方法来做到这一点:

_.chain(SimpleWeapons)
  .omit('properties')
  .sample()
  .get('Name', '') // The extra '' is in case the .Name property is undefined.
  .value();
于 2018-03-14T01:21:30.287 回答
1

如果您只想要一个结果,请使用_.sample来获取一个随机项目。我还会_.omit用来确保您不拉properties钥匙,这不是有效的武器。

一旦您从_.sample调用中获得了一个随机对象,您就可以通过使用点表示法以通常的方式获取它的名称:.Name.

例子:

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)"
  }
}

const randomWeapon = _.sample(_.omit(SimpleWeapons, "properties")).Name;
console.log("A random weapon:", randomWeapon);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>

于 2018-03-13T20:04:30.950 回答