简体   繁体   English

如何在 JS object 中获取特定嵌套属性的所有值

[英]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.我已经知道 * 在这里对 select 不起作用。 It's my first time using objects.这是我第一次使用对象。 Is it necessary to write a for function for example?例如,是否有必要为 function 写一个? 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.好吧,你有一个moves ( move ) 有其他对象。 You may map each value of the moves object to some nested value.您可以将 map的每个值moves object 到某个嵌套值。

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.您可以使用Object.values (它返回一个包含给定对象的所有值的数组),然后使用Array.prototype.map进行映射。

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:理想情况下,您的 move 属性将是一个数组,但是,鉴于您拥有的数据(并假设每个“attackN”属性都是一个 object 具有一个称为损坏的单个属性,那么:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 从嵌套的 object 中获取属性的所有值 - Typescript get all values of a property from a nested object 如何获取 object 中属性名称的所有值? - How to get all the values of a property name in an object? 如何在嵌套的 JS 对象中查找特定属性的总和 - How to find sum of a specific property in nested JS object 如何在AngularJs中获取json对象和嵌套对象属性名称和值? - How to get json object and nested object Property names and values in AngularJs? 我们如何从具有名称 ID 但具有特定父对象的 JSON 中获取所有属性值 - How do we get all property values from a JSON with name ID but has a specific parent object 如何使用javascript获取嵌套对象中所有子项的单个属性? - How to get a single property of all children in a nested object using javascript? vue.js-如何观察嵌套对象的属性并获取索引? - vue.js - how to watch property of nested object and get index? JS获取名称中带空格的对象的嵌套属性 - JS get nested property of object with space in name 如何获取嵌套JavaScript object中某个键的所有值? - How to get all the values of a key in a nested JavaScript object? 如何在for…in中获得嵌套对象属性的正确属性? - How to get the right property of a nested object property in a for…in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM