繁体   English   中英

如何遍历此对象以获取新对象?

[英]How can I iterate over this object to to get a new object?

我对 Javascript 比较陌生,刚刚学习了基本的 for 循环和 for in 循环。 如何遍历此对象以获取某些键和值? 对象中有一堆嵌套对象,所以我有点困惑。 我正在考虑使用某种“Object...()”方法,但不确定使用哪种方法。

期望的输出结果是一个具有给定冠军键的对象,后跟冠军 id/name = {"266": "Aatrox", "103" : "Ahri"}

下面的示例输入

  "type": "champion",
  "format": "standAloneComplex",
  "version": "10.16.1",
  "data": {
    "Aatrox": {
      "version": "10.16.1",
      "id": "Aatrox",
      "key": "266",
      "name": "Aatrox",
      "title": "the Darkin Blade",
      "blurb": "Once honored defenders of Shurima against the Void, Aatrox and his brethren would eventually become an even greater threat to Runeterra, and were defeated only by cunning mortal sorcery. But after centuries of imprisonment, Aatrox was the first to find...",
      "info": {
        "attack": 8,
        "defense": 4,
        "magic": 3,
        "difficulty": 4
      },
      "image": {
        "full": "Aatrox.png",
        "sprite": "champion0.png",
        "group": "champion",
        "x": 0,
        "y": 0,
        "w": 48,
        "h": 48
      },
      "tags": [
        "Fighter",
        "Tank"
      ],
      "partype": "Blood Well",
      "stats": {
        "hp": 580,
        "hpperlevel": 90,
        "mp": 0,
        "mpperlevel": 0,
        "movespeed": 345,
        "armor": 38,
        "armorperlevel": 3.25,
        "spellblock": 32.1,
        "spellblockperlevel": 1.25,
        "attackrange": 175,
        "hpregen": 3,
        "hpregenperlevel": 1,
        "mpregen": 0,
        "mpregenperlevel": 0,
        "crit": 0,
        "critperlevel": 0,
        "attackdamage": 60,
        "attackdamageperlevel": 5,
        "attackspeedperlevel": 2.5,
        "attackspeed": 0.651
      }
    },
    "Ahri": {
      "version": "10.16.1",
      "id": "Ahri",
      "key": "103",
      "name": "Ahri",
      "title": "the Nine-Tailed Fox",
      "blurb": "Innately connected to the latent power of Runeterra, Ahri is a vastaya who can reshape magic into orbs of raw energy. She revels in toying with her prey by manipulating their emotions before devouring their life essence. Despite her predatory nature...",
      "info": {
        "attack": 3,
        "defense": 4,
        "magic": 8,
        "difficulty": 5
      },
      "image": {
        "full": "Ahri.png",
        "sprite": "champion0.png",
        "group": "champion",
        "x": 48,
        "y": 0,
        "w": 48,
        "h": 48
      },
      "tags": [
        "Mage",
        "Assassin"
      ],
      "partype": "Mana",
      "stats": {
        "hp": 526,
        "hpperlevel": 92,
        "mp": 418,
        "mpperlevel": 25,
        "movespeed": 330,
        "armor": 20.88,
        "armorperlevel": 3.5,
        "spellblock": 30,
        "spellblockperlevel": 0.5,
        "attackrange": 550,
        "hpregen": 6.5,
        "hpregenperlevel": 0.6,
        "mpregen": 8,
        "mpregenperlevel": 0.8,
        "crit": 0,
        "critperlevel": 0,
        "attackdamage": 53.04,
        "attackdamageperlevel": 3,
        "attackspeedperlevel": 2,
        "attackspeed": 0.668
      }
    }```

 const obj = { name: 'Sam', old: 30, nested: { data: 'ABC', time: '3:15' } } for(let i in obj){ console.log(i); //returns values console.log(obj[i]); //returns keys for(let j in obj[i]){ console.log(j); //returns values of nested 2d level console.log(obj[i][j]); //returns keys of nested 2d level } }

暂无
暂无

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

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