带有嵌套对象的 Lodash ._sample

Lodash ._sample with nested objects

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

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

}

我想随机 return 嵌套对象属性之一(作为字符串),因此 "Club" 或 "Dagger" 使用函数。我以下列方式在这个项目的扁平对象中使用了 _.sample_.sampleSize

var getDefaultEquipment = (chaClass) => {
    if(chaClass === "Bard"){
        var equipment = {};
        equipment.equipment = (_.sampleSize(classes.Bard.equipment,1));
        return equipment;}}

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

如果您只想要一个结果,请使用 _.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>

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

_.chain(SimpleWeapons)
  .omit('properties')
  .sample()
  .get('Name', '') // The extra '' is in case the .Name property is undefined.
  .value();