0

I have an object with nested objects like this:

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

}

I would like to return one of the nested object properties (as as string) at random, so "Club" or "Dagger" using a function. I have used _.sample and _.sampleSize in flatter objects in this project in the following manner:

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

but I'm unsure how to dig a little deeper, or even if it's possible?

Christopher Long
  • 854
  • 4
  • 11
  • 21
  • I'm not sure what you're asking. It looks like just `_.sampleSize(SimpleWeapons, 1)` would give you what you want. Or if you only wanted the key, `_sampleSize(_.keys(SimpleWeapons), 1)`. If that's not right, can you given an example of what exactly you would want returned to you from? – CRice Mar 13 '18 at 19:54
  • @CRice , Sorry if wasn't clear. For example, I would like to retrieve the `Name:` property of Club, so "Club" as a string. currently `equipment.startingWeapons = (_.sampleSize(classes.Bard.startingWeapons,1));` returns an object – Christopher Long Mar 13 '18 at 20:01

2 Answers2

2

I know there is already an accepted answer, but I wanted to also show how you can do this with the _.chain() method as well:

_.chain(SimpleWeapons)
  .omit('properties')
  .sample()
  .get('Name', '') // The extra '' is in case the .Name property is undefined.
  .value();
th3n3wguy
  • 3,649
  • 2
  • 23
  • 30
1

If you only want one result, use _.sample to get one random item. I would also use _.omit to make sure that you don't pull the properties key, which isn't a valid weapon.

Once you have a random object from the _.sample call, you can get its name in the usual way by using dot notation: .Name.

Example:

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>
CRice
  • 29,968
  • 4
  • 57
  • 70