第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

優(yōu)化嵌套for循環(huán)算法,Javascript

優(yōu)化嵌套for循環(huán)算法,Javascript

我正在使用 pokemon API 構(gòu)建一個(gè)有趣的小信息應(yīng)用程序。我特別想要一個(gè)部分詳細(xì)說(shuō)明口袋妖怪的傷害關(guān)系。我目前檢索數(shù)據(jù)并將其格式化為:我想要做的是像這樣格式化它。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",  ],}數(shù)據(jù)被格式化到這個(gè)地圖中,沒(méi)有重復(fù)的類型。這是我目前的解決方案。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;  };這太殘忍了……我知道這一點(diǎn)。我的問(wèn)題是迄今為止任何優(yōu)化它的嘗試都失敗了。我曾嘗試使用map和reduce函數(shù)的組合,但無(wú)濟(jì)于事。如果有人可以看一下并優(yōu)化它,將不勝感激!
查看完整描述

3 回答

?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以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)



查看完整回答
反對(duì) 回復(fù) 2023-03-03
?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊

這行得通嗎?


我不知道從主數(shù)組中調(diào)用每個(gè)子元素是什么,values所以thing我使用的是什么。但這只是在那里挖掘并從每個(gè)子級(jí)別元素中提取名稱并將它們相加。


我不擔(dān)心唯一性,但你可以(在 concat 之后)添加一個(gè) 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

},{});


查看完整回答
反對(duì) 回復(fù) 2023-03-03
?
暮色呼如

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊

使用Array.reduce收集數(shù)據(jù),使用Array#forEach遍歷元素,使用Object.entries從對(duì)象中獲取鍵/值。

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/或試試這個(gè)(用洞代碼):


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


查看完整回答
反對(duì) 回復(fù) 2023-03-03
  • 3 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)