I know similar questions were asked, but none of them helped me with my problem.
I have a dynamic array of objects I get from server (more draws in the future):
0: {id: 19, draw: 8, colour: 'Black', person1: 'John', person2: 'Jane'}
1: {id: 18, draw: 8, colour: 'Red', person1: 'Mike', person2: 'Mary'}
2: {id: 20, draw: 8, colour: 'White', person1: 'Sam', person2: 'Leah'}
3: {id: 16, draw: 7, colour: 'Red', person1: 'Bob', person2: 'Nora'}
4: {id: 17, draw: 7, colour: 'Black', person1: 'Tom', person2: 'Tina'}
I reorder the array into multiple arrays based on draw number:
getDrawHistory(): void {
this.pairApiService.getDrawHistory().subscribe(pairs => {
this.pairs = pairs;
const groupBy = (key: string) => (array: any[]) =>
array.reduce((objectsByKeyValue: { [x: string]: any; }, obj: { [x: string]: any; }) => {
const value = obj[key];
objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
return objectsByKeyValue;
}, {})
const groupByDraw = groupBy('draw');
this.pairsByDraw = JSON.parse(JSON.stringify({ pairsByDraw: groupByDraw(this.pairs) }, null, 2));
})
}
Where I get:
pairsByDraw:
7: Array(2)
0: {id: 16, draw: 7, colour: 'Red', person1: 'Bob', person2: 'Nora'}
1: {id: 17, draw: 7, colour: 'Black', person1: 'Tom', person2: 'Tina'}
8: Array(3)
0: {id: 18, draw: 8, colour: 'Red', person1: 'Mike', person2: 'Mary'}
1: {id: 19, draw: 8, colour: 'Black', person1: 'John', person2: 'Jane'}
2: {id: 20, draw: 8, colour: 'White', person1: 'Sam', person2: 'Leah'}
What I want is to iterate the array of arrays of objects with *ngFor
and get something like (possibly dynamically generated mutliple tables):
Red | Black |
---|---|
Bob | Tom |
Nora | Tina |
Red | Black | White |
---|---|---|
Mike | John | Sam |
Mary | Jane | Leah |
How is it done?
I tried:
*ngFor="let item of pairsByDraw.pairsByDraw | keyvalue"
{{item.key}}: {{item.value}}
but cannot get anything from item.value
other than [object Object]
.
EDIT: Changing the brackets from in:
const groupBy = (key: string) => (array: any[]) =>
array.reduce((objectsByKeyValue: { [x: string]: any; }, obj: { [x: string]: any; }) => {
const value = obj[key];
objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
return objectsByKeyValue;
}, []) // before: }, {})
from {}
to []
seems to make some difference. Will dig into.