优化嵌套for循环算法,Javascript

我正在使用 pokemon API 构建一个有趣的小信息应用程序。我特别想要一个部分详细说明口袋妖怪的伤害关系。我目前检索数据并将其格式化为:

我想要做的是像这样格式化它。


DamageMap:  Map {

  "double_damage_from" => Array [

    "flying",

    "poison",

    "bug",

    "fire",

    "ice",

    "ground",

    "psychic",

  ],

  "double_damage_to" => Array [

    "ground",

    "rock",

    "water",

    "grass",

    "fairy",

  ],

  "half_damage_from" => Array [

    "ground",

    "water",

    "grass",

    "electric",

    "fighting",

    "poison",

    "bug",

    "fairy",

  ],

  "half_damage_to" => Array [

    "flying",

    "poison",

    "bug",

    "steel",

    "fire",

    "grass",

    "dragon",

    "ground",

    "rock",

    "ghost",

  ],

  "no_damage_from" => Array [],

  "no_damage_to" => Array [

    "steel",

  ],

}

数据被格式化到这个地图中,没有重复的类型。这是我目前的解决方案。


const keys = [

      "double_damage_from",

      "double_damage_to",

      "half_damage_from",

      "half_damage_to",

      "no_damage_from",

      "no_damage_to",

    ];

    const damageMap = new Map();


    for (let i = 0; i < results.length; ++i) {

      for (let j = 0; j < keys.length; ++j) {

        if (!damageMap.has(keys[j])) {

          damageMap.set(keys[j], []);

        }

        const val = damageMap.get(keys[j]);

        const curr = results[i][keys[j]];


        for (let k = 0; k < curr.length; ++k) {

          if (val.indexOf(curr[k].name) === -1) {

            val.push(curr[k].name);

          }

        }

        damageMap.set(keys[j], val);

      }

    }


    return damageMap;

  };

这太残忍了……我知道这一点。我的问题是迄今为止任何优化它的尝试都失败了。我曾尝试使用map和reduce函数的组合,但无济于事。如果有人可以看一下并优化它,将不胜感激!


三国纷争
浏览 126回答 3
3回答

SMILET

您可以Array.prototype.reduce()与一起使用Array.prototype.forEach():const src = [{"double_damage_from":[{"name":"flying","url":"https://pokeapi.co/api/v2/type/3/",},{"name":"poison","url":"https://pokeapi.co/api/v2/type/4/",},{"name":"bug","url":"https://pokeapi.co/api/v2/type/7/",},{"name":"fire","url":"https://pokeapi.co/api/v2/type/10/",},{"name":"ice","url":"https://pokeapi.co/api/v2/type/15/",},],"double_damage_to":[{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/",},{"name":"rock","url":"https://pokeapi.co/api/v2/type/6/",},{"name":"water","url":"https://pokeapi.co/api/v2/type/11/",},],"half_damage_from":[{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/",},{"name":"water","url":"https://pokeapi.co/api/v2/type/11/",},{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/",},{"name":"electric","url":"https://pokeapi.co/api/v2/type/13/",},],"half_damage_to":[{"name":"flying","url":"https://pokeapi.co/api/v2/type/3/",},{"name":"poison","url":"https://pokeapi.co/api/v2/type/4/",},{"name":"bug","url":"https://pokeapi.co/api/v2/type/7/",},{"name":"steel","url":"https://pokeapi.co/api/v2/type/9/",},{"name":"fire","url":"https://pokeapi.co/api/v2/type/10/",},{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/",},{"name":"dragon","url":"https://pokeapi.co/api/v2/type/16/",},],"name":"grass","no_damage_from":[],"no_damage_to":[],},{"double_damage_from":[{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/",},{"name":"psychic","url":"https://pokeapi.co/api/v2/type/14/",},],"double_damage_to":[{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/",},{"name":"fairy","url":"https://pokeapi.co/api/v2/type/18/",},],"half_damage_from":[{"name":"fighting","url":"https://pokeapi.co/api/v2/type/2/",},{"name":"poison","url":"https://pokeapi.co/api/v2/type/4/",},{"name":"bug","url":"https://pokeapi.co/api/v2/type/7/",},{"name":"grass","url":"https://pokeapi.co/api/v2/type/12/",},{"name":"fairy","url":"https://pokeapi.co/api/v2/type/18/",},],"half_damage_to":[{"name":"poison","url":"https://pokeapi.co/api/v2/type/4/",},{"name":"ground","url":"https://pokeapi.co/api/v2/type/5/",},{"name":"rock","url":"https://pokeapi.co/api/v2/type/6/",},{"name":"ghost","url":"https://pokeapi.co/api/v2/type/8/",},],"name":"poison","no_damage_from":[],"no_damage_to":[{"name":"steel","url":"https://pokeapi.co/api/v2/type/9/",},],},],&nbsp; &nbsp; result = src.reduce((acc, o) => {&nbsp; &nbsp; &nbsp; Object.keys(o).forEach(key => {&nbsp; &nbsp; &nbsp; &nbsp; if(Array.isArray(o[key])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const match = acc.get(key),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items = o[key].map(({name}) => name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match ?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match.push(...items) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acc.set(key, items)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;})&nbsp;&nbsp; &nbsp; &nbsp; return acc&nbsp; &nbsp; }, new Map)&nbsp; &nbsp;&nbsp;console.log(result)

慕森王

这行得通吗?我不知道从主数组中调用每个子元素是什么,values所以thing我使用的是什么。但这只是在那里挖掘并从每个子级别元素中提取名称并将它们相加。我不担心唯一性,但你可以(在 concat 之后)添加一个 uniq 清理东西。// where `values` is your initial data structureconst damageMap = values.reduce((memo, thing) => {&nbsp; Object.keys(thing).forEach(key => {&nbsp; &nbsp; if (key !== 'name') {&nbsp; &nbsp; &nbsp; memo[key] = memo[key] || []&nbsp; &nbsp; &nbsp; memo[key] = memo[key].concat(&nbsp; &nbsp; &nbsp; &nbsp; thing[key].map(({name}) => name))&nbsp; &nbsp; }&nbsp; })&nbsp; return memo},{});

暮色呼如

使用Array.reduce收集数据,使用Array#forEach遍历元素,使用Object.entries从对象中获取键/值。let map = arr.reduce((acc, cur) => {&nbsp; &nbsp;Object.entries(cur).forEach(([key, values]) => {&nbsp; &nbsp; &nbsp; &nbsp;if (!acc[key] && key !=='name') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;acc[key] = [];&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;if (typeof(values)=== 'object') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;values.forEach(({name}) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;acc[key].push(name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;});&nbsp; &nbsp;return acc;}, {});在这里玩 around&nbsp;https://jsfiddle.net/h8kL5gp9/或试试这个(用洞代码):let arr = [&nbsp; &nbsp;{&nbsp; &nbsp; "double_damage_from": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "flying",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/3/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "poison",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/4/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "bug",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/7/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "fire",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/10/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "ice",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/15/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; "double_damage_to": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "ground",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/5/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "rock",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/6/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "water",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/11/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; "half_damage_from": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "ground",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/5/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "water",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/11/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "grass",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/12/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "electric",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/13/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; "half_damage_to": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "flying",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/3/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "poison",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/4/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "bug",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/7/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "steel",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/9/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "fire",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/10/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "grass",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/12/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "dragon",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/16/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; "name": "grass",&nbsp; &nbsp; "no_damage_from": [],&nbsp; &nbsp; "no_damage_to": [],&nbsp; },&nbsp; {&nbsp; &nbsp; "double_damage_from": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "ground",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/5/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "psychic",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/14/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; "double_damage_to": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "grass",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/12/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "fairy",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/18/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; "half_damage_from": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "fighting",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/2/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "poison",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/4/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "bug",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/7/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "grass",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/12/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "fairy",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/18/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; "half_damage_to": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "poison",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/4/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "ground",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/5/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "rock",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/6/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "ghost",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/8/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; &nbsp; "name": "poison",&nbsp; &nbsp; "no_damage_from": [],&nbsp; &nbsp; "no_damage_to": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "name": "steel",&nbsp; &nbsp; &nbsp; &nbsp; "url": "https://pokeapi.co/api/v2/type/9/",&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; ],&nbsp; },];let map = arr.reduce((acc, cur) => {&nbsp; &nbsp;Object.entries(cur).forEach(([key, values]) => {&nbsp; &nbsp; &nbsp; &nbsp;if (!acc[key] && key !=='name') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;acc[key] = [];&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;if (typeof(values)=== 'object') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;values.forEach(({name}) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;acc[key].push(name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;});&nbsp; &nbsp;return acc;}, {});console.log(map);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript