过滤嵌套对象

时间:2018-10-04 19:56:38

标签: javascript ecmascript-6

我正在尝试根据角色数组(即对象数组)中的特定roleType过滤列表。

下面是我的json

this.rolesData =  [{
         "firstName": "Francis",
         "lastName": "Underwood",
         "middleName": "",
         "title": { "titleId": "1", "titleName": "AA" },
         "roles": [
             {"roleGuid" : 1,  "roleType" : 1 },  
             { "roleGuid" : 2,  "roleType" : 3 }
        ]
    },
    {
        "firstName": "Claire",
        "lastName": "Underwood",
        "middleName": "",
        "title": { "titleId": "2", "titleName": "BB" },
        "roles": [
            {"roleGuid" : 1,  "roleType" : 2 },  
            { "roleGuid" : 2,  "roleType" : 3 }, 
            { "roleGuid" : 3, "roleType" : 4 }
        ]
    }
];

我正在尝试获取其中具有roleType 1的项目。因此对于我的示例,它应该仅返回第一个对象,因为只有第一个对象在其角色列表中具有roleType 1。

我在下面尝试过,但是没有用

this.rolesData.filter(data => {
    data.roles.filter(role => {
        if (role.roleType == 1) {
          return role;
        }
    });
});

1 个答案:

答案 0 :(得分:0)

class PageIndicatorPill extends React.Component{ constructor(props){ super(props); if(this.props.active) this.state={image : onState}; else this.state = { image: offState }; this.style = { backgroundImage:`url(${this.state.image})`, marginRight:'10px', height:"32px", width:"32px" , display:"inline-block"}; } componentWillReceiveProps(nextProps){ if (nextProps.active) this.setState({ image: onState }); else this.setState({image : offState}); } 应该使用一个函数,该函数将为给定项目返回.filtertrue

false
相关问题