为了账号安全,请及时绑定邮箱和手机立即绑定

优化嵌套for循环算法,Javascript

优化嵌套for循环算法,Javascript

三国纷争 2023-03-03 09:41:19
我正在使用 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函数的组合,但无济于事。如果有人可以看一下并优化它,将不胜感激!
查看完整描述

3 回答

?
SMILET

TA贡献1796条经验 获得超4个赞

您可以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/",},],},],



    result = src.reduce((acc, o) => {

      Object.keys(o).forEach(key => {

        if(Array.isArray(o[key])){

          const match = acc.get(key),

                items = o[key].map(({name}) => name)

          match ? 

          match.push(...items) :

          acc.set(key, items)

        }

       }) 

      return acc

    }, new Map)

    

console.log(result)



查看完整回答
反对 回复 2023-03-03
?
慕森王

TA贡献1777条经验 获得超3个赞

这行得通吗?


我不知道从主数组中调用每个子元素是什么,values所以thing我使用的是什么。但这只是在那里挖掘并从每个子级别元素中提取名称并将它们相加。


我不担心唯一性,但你可以(在 concat 之后)添加一个 uniq 清理东西。


// where `values` is your initial data structure


const damageMap = values.reduce((memo, thing) => {

  Object.keys(thing).forEach(key => {

    if (key !== 'name') {

      memo[key] = memo[key] || []

      memo[key] = memo[key].concat(

        thing[key].map(({name}) => name))

    }

  })

  return memo

},{});


查看完整回答
反对 回复 2023-03-03
?
暮色呼如

TA贡献1853条经验 获得超9个赞

使用Array.reduce收集数据,使用Array#forEach遍历元素,使用Object.entries从对象中获取键/值。

let map = arr.reduce((acc, cur) => {

   Object.entries(cur).forEach(([key, values]) => {

       if (!acc[key] && key !=='name') {

           acc[key] = [];

       }

       if (typeof(values)=== 'object') {

           values.forEach(({name}) => {

               acc[key].push(name);

           });

       }

   });

   return acc;

}, {});

在这里玩 around https://jsfiddle.net/h8kL5gp9/或试试这个(用洞代码):


let arr = [

   {

    "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/",

      },

    ],

  },

];


let map = arr.reduce((acc, cur) => {

   Object.entries(cur).forEach(([key, values]) => {

       if (!acc[key] && key !=='name') {

           acc[key] = [];

       }

       if (typeof(values)=== 'object') {

           values.forEach(({name}) => {

               acc[key].push(name);

           });

       }

   });

   return acc;

}, {});


console.log(map);


查看完整回答
反对 回复 2023-03-03
  • 3 回答
  • 0 关注
  • 125 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信