1

I am trying to go through my JSON object and add specific fields to their own array.

I am trying to iterate through the below object and store the 'appSupportedId' in its own array.

I am getting an ERROR

ERROR

 core.js:15714 ERROR TypeError: info.flatMap is not a function

I have implemented something similar in my code but the only difference with the backend json object is that this below object has a nested array

Any help would be appreciated!

component.ts

userAppDetails:[];

this.incidentService.get(this.id)
  .subscribe((info) => 
    this.userAppDetails = (info.flatMap(x => x.applicationsSupported)).contactAll().map(y=> y.appSupportedId))

JSON OBJECT

 "incidentNumber": 18817,
 "Email": null,
 "applicationsSupported": [
    {
        "appSupportedId": 18569,           
        "supportAreaId": 122,
        "supportAreas": {
            "applicationId": 122,               
            "activeFlag": "Y",
        },
        "appSupportedName": "app 1"
    },
    {
        "appSupportedId": 18592,          
        "supportAreaId": 123,
        "supportAreas": {
            "applicationId": 123,
            "activeFlag": "Y",
        },
        "appSupportedName": "app 2"
    },
    {
        "appSupportedId": 18655,
        "supportAreaId": 122,
        "supportAreas": {
            "applicationId": 122,
            "activeFlag": "Y",
        },
        "appSupportedName": "app 3"
    }
],
"createdDate": "2020-01-17T18:02:51.000+0000",
4
  • 1
    It looks like info is an object, not an array. I don't understand what do you want. You need to create an array and fill it with each appSupportedId values ? Commented Jan 20, 2020 at 16:17
  • 2
    You mean something like stackblitz.com/edit/rxjs-d9nchy ? You seem to be mixing up observable pipe operators (within a pipe before subscribe) with object operators (within subscribe) Commented Jan 20, 2020 at 16:25
  • @AndrewAllen I think this is the good answer. Maybe you can write it as an answer. Commented Jan 20, 2020 at 16:42
  • @Emilien thought I'd try and clarify first but answer posted :) Commented Jan 20, 2020 at 16:54

1 Answer 1

2

You seem to be mixing up observable pipe operators (within a pipe before subscribe) with object operators (within subscribe).

RxJS pipeable operators work within an observable stream and they are very powerful. You can put a bunch of operators together before you even subscribe. You can sometimes acheive a similar result within the subscribe though this often leads to messy code.

flatMap

concatAll

The following (https://stackblitz.com/edit/rxjs-d9nchy) is an example using the map RxJS operator. note this is different from the array map operator:

import { of } from 'rxjs'; 
import { map } from 'rxjs/operators';

const DATA = {
  "incidentNumber": 18817,
  "Email": null,
  "applicationsSupported": [
      {
          "appSupportedId": 18569,           
          "supportAreaId": 122,
          "supportAreas": {
              "applicationId": 122,               
              "activeFlag": "Y",
          },
          "appSupportedName": "app 1"
      },
      {
          "appSupportedId": 18592,          
          "supportAreaId": 123,
          "supportAreas": {
              "applicationId": 123,
              "activeFlag": "Y",
          },
          "appSupportedName": "app 2"
      },
      {
          "appSupportedId": 18655,
          "supportAreaId": 122,
          "supportAreas": {
              "applicationId": 122,
              "activeFlag": "Y",
          },
          "appSupportedName": "app 3"
      }
  ],
  "createdDate": "2020-01-17T18:02:51.000+0000",
}
const source = of(DATA).pipe(
  map(x => x.applicationsSupported),
  map(arr => arr.map(entry => entry.appSupportedId))
);

source.subscribe(x => console.log(x));
Sign up to request clarification or add additional context in comments.

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.