0

How get the object by filtering key value inside the object? I have objects below where I need to only pull data which has session_id:23

data = [{ name: "fist Name", session_id : "23", login: "date" }, { name: "Second Name", session_id : "18", login: "date" }, { name: "Third Name", session_id : "23", login: "date" }];

I tried Angular Filter and Map method to filter this out but dosent seem to work for me here.
.pipe(
    map((res) => {
        res => res.filter(res => res.season_id == 23)
        console.log(res);
    })
)

ngOnInit() {
    this._service.getData();
    this._service.updatedPlayer
        .pipe(
            map((res) => {
                res => ress.filter(res => res.season_id == 23)
                console.log(res);
            })
        )
        .subscribe(
            (playerSession) => {
                this.playerSessionUpdate = playerSession;
            }
        )

}

2 Answers 2

2

I think you're confusing arrays with streams.

You need to use the filter method of Array, not the filter operator of observables. So if this._service.updatedPlayer contains your observable stream of data, do this:

this._service.updatedPlayer.subscribe(data => {
  this.playerSessionUpdate = data.filter(res => res.season_id == 23);
}

this.playerSessionUpdate will now contain your filtered data.

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

1 Comment

that was my mistake, good point to be noted here array and stream/observables. Thanx for your help Fredrik
0

your pipe method map must return something. So you should actually do

 map((res) => {
          return res.filter(res => res.season_id === 23)
     })

3 Comments

this dosent work for me either, this output straing the function like this: function (ress) { return ress.filter(function (ress) { return ress.season_id == 23; }); }
but I don't understand where does the 'ress' variables comes from. You have a 'res' and a 'ress' in your example. Is it a typo? please clarify so i can try to help more :D
Its res only sir, can we filter this it cannot seem to be filtered with this code.

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.