2

I would like to get an array of arrays like ...[[1,2],[5,8],[9]]... depending on the group by.

This is the example I am following from the rxjs library example. This ex only uses one property to group by.

Observable.of<Obj>({id: 1, age: 5, weight: 145},
                   {id: 9, age: 7, weight: 10},
                   {id: 2, age: 5, weight: 145},
                   {id: 60, age: 11, weight: 145},
    )
    .groupBy(p => p.id)
    .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], []))
    .subscribe(p => console.log(p));

Expected result: [[1,2],[9],[60]]

I haven't managed to make it work with more than one property (age and weight).

Could anybody tell me how I can achieve this?

Thanks,

1 Answer 1

4

you can specify a more detailed function for the group by function and return a unique key. for example age-weights. I put together a jsfiddle https://jsfiddle.net/g5pj065m/3638/

Rx.Observable.of({id: 1, age: 5, weight: 145},
    {id: 9, age: 7, weight: 10},
    {id: 2, age: 5, weight: 145},
    {id: 60, age: 11, weight: 145}
)
.groupBy(p => {
    return `${p.age}- ${p.weight}`;
})
.flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], []))
.subscribe(p => console.log(p));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks that was great! I tried something similar but didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.