簡體   English   中英

Lodash for 循環在使用“i”時返回未定義,但適用於設置值

[英]Lodash for loop returns undefined upon using the “i”, but works with set values

我正在嘗試使用 Lodash 在 object 中搜索某些 integer 值,然后將一些 object 值推送到數組中。 integer 值必須是可變的並且來自不同的數組,但始終返回未定義的值

當我對這樣的值進行硬編碼時: _.find(getChampionList.data, { 'key': '266'})它可以完美運行,但是如果我使用數組中的變量,它會返回未定義。

我試圖從 object 中提取數據:

var getChampionList = {
    "type": "champion",
    "format": "standAloneComplex",
    "version": "9.18.1",
    "data": {
        "Aatrox": {
            "version": "9.18.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": "9.18.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
            }
        }
    }
}

var championIds = [ 266, 103 ]

    for (i = 0; i < championIds.length; i++) {
         var champion = _.find(getChampionList.data, { 'key': championIds[i]})
         console.log(champion) //returns undefined
         championArray.push(champion.id) //creates an error
      }

var champion = _.find(getChampionList.data, { 'key': championIds[i]})行應該返回對象,然后將“id”字段推入“championArray”。 像這樣的硬編碼{ 'key': '266'}它返回 Aatrox 的 object,但使用 ChampionIds championIds[i]它只是給出一個未定義的值(而 ChampionIds[0] 確實返回“266”)。

我不確定我的代碼有什么問題,但可能與 Lodash 或打字錯誤有關? 我已經看了好幾個小時 console.logging 每一行,但看不出有什么問題。

你需要做三件事:

  1. 既然你“知道”你需要找到key值,那就從championData.data開始
  2. 將對象(鍵值對)簡化為列表
  3. 將密鑰解析為 integer

或者,您可以使用 map 和過濾器。

 let championData = getData(), championIds = [ 266, 103 ], championList = Object.keys(championData.data).reduce((list, name) => { return championIds.includes(parseInt(championData.data[name].key, 10))? list.concat(championData.data[name].id): list; }, []); console.log(championList); function getData() { return { "type": "champion", "format": "standAloneComplex", "version": "9.18.1", "data": { "Aatrox": { "version": "9.18.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": "9.18.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 } } } }; }
 .as-console-wrapper { top: 0; max-height: 100%;important; }

您正確使用_.find() Find 不起作用,因為鍵是字符串,而您正在尋找數字:

 var getChampionList = {"type":"champion","format":"standAloneComplex","version":"9.18.1","data":{"Aatrox":{"version":"9.18.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":"9.18.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}}}} var championIds = [266, 103] var championArray = [] for (i = 0; i < championIds.length; i++) { var champion = _.find(getChampionList.data, { 'key': String(championIds[i]) // convert the championIds[i] to String }) championArray.push(champion.id) } console.log(championArray)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

然而,這並不是很有效,因為每個搜索循環都是從頭開始的。 如果您有一個簡短的data收集,您希望看到任何差異。 但是,對於長data ,您可以使用_.intersectionWith()查找具有相關鍵的項目,然后使用_.map()提取id值。

 var getChampionList = {"type":"champion","format":"standAloneComplex","version":"9.18.1","data":{"Aatrox":{"version":"9.18.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":"9.18.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}}}} var championIds = [266, 103] var championArray = _.map( _.intersectionWith( _.values(getChampionList.data), // get an array of data items championIds, (a, b) => a.key == b // use loose equality that ignores the type ), 'id') // extract the id using map console.log(championArray)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM