Rxjs To Combine Attributes From Triples To A Table
I have a service producing objects that are like triples. They will be in this format: { country, attribute, value } Example: { country: 'usa', attribute: 'population', value:
Solution 1:
There are two components in your question, the data structure part, and the data flow part (I suppose you get these data as a stream i.e. one by one, hence the reason why you use Rxjs).
A simple way to iteratively build you data structure is to use the scan
operator. For instance :
myDataStructure$ = dataSource$.scan(function (accDataStructure, triple){
accDataStructure[triple.country] = accDataStructure[triple.country] || {}
accDataStructure[triple.country][triple.attribute] = accDataStructure[triple.country][triple.attribute] || {}
accDataStructure[triple.country][triple.attribute] = triple.valuereturn accDataStructure
}, {})
That makes the assumption that dataSource$ produces objects of the shape { country, attribute, value }
. Then myDataStructure$
will output, for every incoming data, the iteratively built data structure that you are seeking. If you only want that data structure once it is finished building, just add a .last()
to myDataStructure$
.
This is not tested so let me know if that worked
Post a Comment for "Rxjs To Combine Attributes From Triples To A Table"