0

i am having an observable which returns something like this:

"@odata.context": "here is the context URL",
"value": [
        {
            "Id": 1,
            "Value": "A"
        },
        {
            "Id": 2,
            "Value": "B"
        },
        {
            "Id": 3,
            "Value": "C"
        },
        {
            "Id": 4,
            "Value": "A"
        }
    ]
}

Using RxJS i would like to get into the "value" property and use distinct on it to limit the response to something like this:

"@odata.context": "here is the context URL",
"value": [
        {
            "Id": 1,
            "Value": "A"
        },
        {
            "Id": 2,
            "Value": "B"
        },
        {
            "Id": 3,
            "Value": "C"
        }
    ]
}

could you help me with it?

1 Answer 1

2

It pretty much depends on how exactly you want to use this RxJS chain but I hope this leads you the right direction:

const source = of(data)
  .pipe(
    mergeMap(d => d.value),
    distinct(v => v.Value),
    toArray(),
  )
  .subscribe(console.log);

mergeMap will reiterate the array of values as separate emissions where distinct will ignore duplicate Values. Then toArray() will just collect all results.

Live demo: https://stackblitz.com/edit/rxjs-is1zp4

Sign up to request clarification or add additional context in comments.

Comments

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.