我有一些格式的数据:
{ regionalGames1: [ 1, 2, 3, 4, 5 ],
regionalGames2: [ 2, 3, 4, 5, 6 ],
regionalGames3: [ 3, 5, 5, 6, 7 ] }
我如何使用lodash将此数据重新排列为:
{
1: [regionalGames1],
2: [regionalGames1, regionalGames2],
3: [regionalGames1, regionalGames2, regionalGames3],
4: [regionalGames1, regionalGames2, regionalGames3],
...
}
等等?
我正在查看_.groupBy
和_.keyBy
,但似乎无法弄明白。
答案 0 :(得分:1)
您可以使用import {Service2} from "service2.ts";
@Injectable()
export class service1{
constructor(private s2: Service2) {
//get data from service2
//edit data
//return data
}
}
和两个@Injectable()
export class service2{
constructor() {
//return data
}
}
循环
Object.keys()

答案 1 :(得分:1)
无需使用像lodash这样的第三方库:
var data = {
regionalGames1: [1, 2, 3, 4, 5],
regionalGames2: [2, 3, 4, 5, 6],
regionalGames3: [3, 5, 5, 6, 7]
};
var out = {};
Object.keys(data).map(function(item) {
data[item].map(function(id) {
if (!out[id]) out[id] = [];
out[id].push(item);
});
});
console.log(out);