0
 Arr1 [
    {id: 300,
    uploads: [
        {idOne: value},
        {idTwo: value}
       ]
    },
    {blah: value, 
     uploads: [
        {idOne: value},
        {idTwo: value}
       ]
    }
]

This Object is being read like this

 this.activatedRoute.parent.params
      .switchMap((params: Params) => this.someService.getSubmissions(+params['id']))
      .subscribe(submissions => console.log('subs', submissions.map(x=>x)))

results in

The Object as shown above.

And

 this.activatedRoute.parent.params
      .switchMap((params: Params) => this.someService.getSubmissions(+params['id']))
      .subscribe(submissions => console.log('subs', submissions.map(x=>x.id)))

Displays id:300

But when I go

 this.activatedRoute.parent.params
      .switchMap((params: Params) => this.someService.getSubmissions(+params['id']))
      .subscribe(submissions => console.log('subs', submissions.map(x=>x.uploads)))

It logs undefined. And I can not reasonably detect why.

This is within the onInit() of an Angular component.

  • 2
    What's the purpose of `.map(x=>x)` ? – Serge K. Jan 22 '18 at 11:04
  • Also, `myObjectName` is an array. It seems to contains some objects. Do those objects all have `uploads` property ? – Serge K. Jan 22 '18 at 11:07
  • to be able to .map(x=>x.id), as submissions.id is not working. – Fabioo Fabiulous Jan 22 '18 at 11:08
  • Yes they do all have the uploads property. – Fabioo Fabiulous Jan 22 '18 at 11:08
  • 1
    You should read [`map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) documentation. `.map(x => x)` will copy your array, which is useless in your case since you call `map` a second time. Also, given the pseudo-code you provided, no, `uploads` is _NOT_ inside all your objects. – Serge K. Jan 22 '18 at 11:10
  • @FabiooFabiulous do not write `(params: Params) => ...`. it is dangerous. Also the identity mapping is nonsense as Serge K already noted. – Aluan Haddad Jan 22 '18 at 11:10
  • @AluanHaddad care to explain why is that dangerous? – Jota.Toledo Jan 22 '18 at 11:14
  • @Jota.Toledo imagine that `params` is typed as `Observable`. If so, any annotation on `params` will pass the type checker since `any` is assignable to and from any other type. This will mislead the reader since it reads as if the `Observable` has a meaningful element type (just redundantly overspecified) but in reality it is acting like a type assertion. An explicit type assertion is much better practice. – Aluan Haddad Jan 22 '18 at 11:17
  • Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – Serge K. Jan 22 '18 at 13:08

1 Answers1

0

Try this, updated the above with forEach:-

this.activatedRoute.parent.params.switchMap(params => {
 this.someService.getSubmissions(+params['id']))
 .subscribe(submissions => {
   submissions.forEach(x=>{
      if(x.id){
        console.log("id",x.id);
      }
      if(x.uploads){
      console.log("uploads",x.uploads);
      }

    })
 });
});
crystalthinker
  • 1,200
  • 15
  • 30