2

I am trying to iterate through my array of objects and add specific fields in to their own array for which I can possibly use for list boxes and other front end stuff.

I am trying to iterate through this object and put objects 'appSupportedId' into its own array for instance I would want it to look like

array of appsSupported = [5,6,15,etc]

I am trying to do this with my http get service and using RXJS observables to do so. Any suggestions would be appreciated on how to get this to happen, thanks.

JSON Object

    [
{
    "applicationUserSubscriptionUniqueId": 18639,
    "createdByName": "2222",
    "updatedDate": "2019-12-02T19:17:45.000+0000",
    "applicationsSupported": {
        "appSupportedId": 5,
        "supportAreaId": 123,
        "supportAreas": {
            "applicationId": 123,
        },
        "appSupportedName": "app1"
    },
    "userSubscriptionInformation": {
        "userSubscribedUniqueId": 18638,
    },
    "reportSubscriptionId": 18638,

 },
 {
    "applicationUserSubscriptionUniqueId": 18638,
    "createdByName": "2222",
    "updatedDate": "2019-12-02T19:17:45.000+0000",
    "applicationsSupported": {
        "appSupportedId": 6,
        "supportAreaId": 123,
        "supportAreas": {
            "applicationId": 123,
        },
        "appSupportedName": "app2"
    },
    "userSubscriptionInformation": {
        "userSubscribedUniqueId": 18638,
    },
    "reportSubscriptionId": 18638,

},
{
    "applicationUserSubscriptionUniqueId": 18637,
    "createdByName": "2222",
    "updatedDate": "2019-12-02T19:17:45.000+0000",
    "applicationsSupported": {
        "appSupportedId": 15,
        "supportAreaId": 123,
        "supportAreas": {
            "applicationId": 123,
        },
        "appSupportedName": "app3"
    },
    "userSubscriptionInformation": {
        "userSubscribedUniqueId": 18638,
    },
    "reportSubscriptionId": 18638,

},
]

3 Answers 3

1

Try it using flatMap:

appsSupported = [];

constructor() {
  this.appsSupported = (this.data.flatMap(x => x.applicationsSupported)).map(y=> y.appSupportedId)
}

Working Demo

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

3 Comments

but this object is coming from a HTTP GET service which is coming through as an Observable and I cannot just assign it to an array like the demo suggests
Why not? After subscribe, add, this.data = resp;and then add this code I shared
excuse my ignorance....i may need more guidence...like this? this.appUserSubService.getSpecificUser(this.userId) .subscribe(info => this.appsSupported = (info.flatMap(x => x.applicationsSupported)).map(y=> y.appSupportedId) ) console.log(this.appsSupported)
1

Just use rxjs map with array with

const supportedAppIds$ = this.http.get<YourType[]>(YOURPATH).pipe(
    map((data: YourType[]) => data.map(entry => entry.applicationsSupported.appSupportedId))
);

Comments

0

Use the rxjs map operator, see my example.

const supportedApps$ = this.http.get('http://example.com').pipe(
    map((el: MyAppData) => el.applicationsSupported.appSupportedId)
);

Then subscribe to supportedApps$, for example in your template.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.