我看了几个例子,但是我不明白。我有一个平面数据集,我想将其转换成打字稿中的嵌套结构。我理解/相信我将需要使用.map和.forEach,但我对此却一无所知。
我有这样的数据:
New.CleanUp.ReferenceOTU120578
,我希望它看起来像:
origData = [
{name: 'Grass', value: 'bulbasaur-0', viewValue: 'Bulbasaur'},
{name: 'Grass', value: 'oddish-1', viewValue: 'Oddish'},
{name: 'Grass', value: 'bellsprout-2', viewValue: 'Bellsprout'},
{name: 'Water', value: 'squirtle-3', viewValue: 'Squirtle'},
{name: 'Water', value: 'psyduck-4', viewValue: 'Psyduck'},
{name: 'Water', value: 'horsea-5', viewValue: 'Horsea'},
{name: 'Fire', value: 'charmander-6', viewValue: 'Charmander'},
{name: 'Fire', value: 'vulpix-7', viewValue: 'Vulpix'},
{name: 'Fire', value: 'flareon-8', viewValue: 'Flareon'},
{name: 'Psychic', value: 'mew-9', viewValue: 'Mew'},
{name: 'Psychic', value: 'mewtwo-10', viewValue: 'Mewtwo'},
];
答案 0 :(得分:0)
如果您使用Underscore之类的实用程序库,这会容易得多。
import * as _ from "underscore";
let groups1 = _.groupBy(origData, (p) => p.name);
let pokemonGroups = Object.keys(groups1).map((type) =>
({ name: type, pokemon: groups1[type].map(({name, ...rest}) => rest) }));