简体   繁体   中英

How to get all values of specific nested property in JS object

I can't seem to find an answer I'm satisfied with, so sorry if this question is a duplicate question, maybe I don't even know how to phrase what I want to achieve.

So anyway this is example code:

var characters = {
  hero: {
    name: 'player',
    hp: 100,
    moves: {
      attack1: {
        damage: 10
      },
      attack2: {
        damage: 20
      },
      attack3: {
        damage: 30
      }
    }
  }
}

What I want to do is write a selector for damage values to split them in half or do any other math on all of them at once. For now I've been checking different syntaxes with console log

console.log(characters.hero.moves[what here?].damage);

but I don't know what to do next, to get only integers, and edit them. I already know * doesn't work to select all here. It's my first time using objects. Is it necessary to write a for function for example? I don't want it to become array or anything as I saw some answers using them. I just want to get to those values and change them all at once to multiply, divide, increase or decrease them.

 //fixed error:D var characters = { hero: { name: 'player', hp: 100, moves: { attack1: { damage: 10 }, attack2: { damage: 20 }, attack3: { damage: 30 } } } } //the following function takes in ONE function as its argument //this argument function is given a number(a damage number) //the number the argument function returns become the new damage number function editDmg(fn){ var moves=characters.hero.moves Object.keys(moves).forEach(a=>moves[a].damage=fn(moves[a].damage)) } //example usage editDmg( n=>n*2 //multiplies all dmg values by 2(you can do any math equation like n=>n_based_equation_here) ) console.log(characters) //second use to prove i've fixed it editDmg(n=>n/2) console.log("for a second time",characters)

Well, you have an object ( moves ) that has other objects. You may map each value of the moves object to some nested value.

You may use Object.values (which returns an array with all the values of the given object) and then Array.prototype.map to do the mapping.

Something like:

 const characters = { hero: { name: 'player', hp: 100, moves: { attack1: { damage: 10 }, attack2: { damage: 20 }, attack3: { damage: 30 } } } }; const movesDamage = Object.values(characters.hero.moves).map((move) => move.damage); console.log(movesDamage); //=> [10, 20, 30]

Ideally, your moves property would be an array, but, given the data you have (and assuming that every 'attackN' property is an object with a single property called damage, then:

var movesKeys = Object.keys(characters.hero.moves);

for (var key in movesKeys)
{
    var attackObject = characters.hero.moves[key];
    attackObject.damage = attackObject.damage * 2; // double the damage
}

To Sum all damage values:

characters.hero.moves.attack1.damage + characters.hero.moves.attack2.damage + characters.hero.moves.attack3.damage

If you dont know how many attaks the hero has use this:

var total_damage = 0;
Object.keys(characters.hero.moves).forEach(function(key) {
  total_damage += characters.hero.moves[key].damage;
});
console.log(total_damage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM