1

I am calling two API to return objects of data. Than run through for each of them and search if it has a value.

I want to check if one of these obj has a vas value matches.

getdata(slug){

  this._apiService.getPages().subscribe(data =>{
    this.pageobj = data
    console.log('this page obj',this.pageobj)
  })


this._apiService.getPosts().subscribe(data =>{
  this.postsobj = data; 
   console.log('this post  obj',this.postsobj)
})

}


this.pageobj is an object


enter image description here


this.postsobj

enter image description here

in both responses they had a property 'slug'. I would like to check if in this.postsobj or this.pageobj has an object that contains 'slug' == 'hello-word', if so to return me object and store in var this.content

UPDATE

export class PageSingleComponent implements OnInit {

page: Page;
pageobj:any;
postsobj:any;
pageobjCheck:any
postsobjCheck:any
pageSlug:any;
content =new Array<any>();
  constructor( private _apiService: apiService, private route: ActivatedRoute ) { }



  getdata(slug){

      this._apiService.getPages().subscribe(data =>{
        this.pageobj = data

        this.content.push(_.filter(this.pageobj, { 'slug':' hello-world' }));


      })


    this._apiService.getPosts().subscribe(data =>{
      this.postsobj = data; 

      this.content.push(_.filter(this.postsobj, { 'slug':' hello-world' }));

    })
   }

  ngOnInit() {

        this.route.params.forEach((params: Params) => {
          // Get slug from the rout
           let slug = params['pageslug'];
           console.log('slug is catcheds', slug)
           this.pageSlug = params['pageslug'];
          this.getdata(slug)


          // Run functions
      //     
        });

    }
 }
1
  • this can be done at server side itself.. any reason handling it here? Commented Apr 16, 2017 at 20:53

2 Answers 2

2

I think you want to use Filter function.

In your callback function passed to map function you want to check whether your response object form array has slug property which is equal to 'hello world'. Your code will like like this:

var content = response.filter(obj => obj && obj.slug === 'hello-world');
Sign up to request clarification or add additional context in comments.

Comments

1

I prefer using lodash as below,

this.content =new Array<any>();
this.content.push(_.filter(this.pageobj, { 'slug':' hello-world' });
this.content.push(_.filter(this.postsobj, { 'slug':' hello-world' });

Alternatively you can handle it in the service using takeWhile operator

getPages(){
   return  this.http.get(...)
                    .takeWhile(data=>{
                        if(data.slug ==== 'hello-world'){
                           return data;
                        }
                    })
}

6 Comments

I don't think you need a 'this' to this.content ?
I don't you need what does this mean?
sorry Aravind. Two things: 1) I think you are missing a ')' at the end of each push. and than I can define the variable like this content =new Array<any>(); and don't need to do this.content =new Array<any>();
sorry for the typo, without initialising an array you cannot push data. you will get undefined error
I am getting an empty object. Did I do it right like I posted above in the update?
|

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.