简体   繁体   English

按对象内的值按角度排序数组对象

[英]Angular Sort Array Object by value inside object

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 我在下面需要仅提取具有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" }]; 数据= [{名称:“拳头名称”,session_id:“ 23”,登录名:“日期”},{名称:“第二名称”,session_id:“ 18”,登录名:“日期”},{名称:“第三名名称”,session_id:“ 23”,登录名:“日期”}];

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;
            }
        )

}

I think you're confusing arrays with streams. 我认为您正在将数组与流混淆。

You need to use the filter method of Array, not the filter operator of observables. 您需要使用Array的filter方法,而不是observables的filter运算符。 So if this._service.updatedPlayer contains your observable stream of data, do this: 因此,如果this._service.updatedPlayer包含您可观察的数据流,请执行以下操作:

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

this.playerSessionUpdate will now contain your filtered data. this.playerSessionUpdate现在将包含您过滤的数据。

your pipe method map must return something. 您的管道方法map必须返回某些内容。 So you should actually do 所以你应该做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM