我绞尽脑汁想弄清楚这一点。我有一个用户的对象模型。嵌套在对象中的是一个名为roles的属性,它是一个角色数组,如下所示:
0: {…}
createdAt: "2020-09-15T22:23:18.551Z"
id: "5f16c3781f2bc30ad20452c8"
roles: Array [ "Owner" ]
updatedAt: "2020-09-15T22:23:18.551Z"
user: Object { id: "5f16c3781f2bc30ad20452c8", nodeId: "5f16c3781f2bc30ad20452c8", username: "John smith", … }
1: {…}
createdAt: "2020-10-20T11:09:07.395Z"
id: "5ea683231fac57758ae7e4e2"
roles: Array [ "Member" ]
updatedAt: "2020-10-20T11:09:07.395Z"
user: Object { id: "5ea683231fac57758ae7e4e2", nodeId: "MDQ6VXNlcjI1MTc0ODQ=", username: "monstertrux", … }
我正在尝试返回具有角色Owner
的用户的索引位置。我尝试了各种各样的方法,但都没有成功。我能做的就是let projOwner = membertempArray.indexOf(item => (item.roles.filter( role => role === 'Owner')
以返回索引值,但如果只是失败,则返回-1。知道我做错了什么吗?
tab.findIndex(item=>item.roles.includes('Owner'))