-
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/",},],},], 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)
-
慕森王
这行得通吗?我不知道从主数组中调用每个子元素是什么,values所以thing我使用的是什么。但这只是在那里挖掘并从每个子级别元素中提取名称并将它们相加。我不担心唯一性,但你可以(在 concat 之后)添加一个 uniq 清理东西。// where `values` is your initial data structureconst 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},{});
-
暮色呼如
使用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);