Javascript 打字稿:按值检查数组中是否存在对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51498419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:50:53  来源:igfitidea点击:

Typescript : Check if object exist in array by value

javascriptarraysangulartypescriptecmascript-6

提问by firasKoubaa

I have this data:

我有这个数据:

roles = [
{roleId: "69801", role: "ADMIN"}
{roleId: "69806", role: "SUPER_ADMIN"}
{roleId: "69805", role: "RB"}
{roleId: "69804", role: "PILOTE"}
{roleId: "69808", role: "VENDEUR"}
{roleId: "69807", role: "SUPER_RB"}
]

i have to filter my table to check if there is an object containing a specifie value of role .

我必须过滤我的表以检查是否有包含角色特定值的对象。

My function should look like this :

我的函数应该是这样的:

checkRoleExistence(role){

// if role exists on one of the objects return true
// else returne false
}

to use it i would do s.th like this :

要使用它,我会这样做:

let ifExists = this.checkRoleExistence("PILOTE") ;

I would like to use the "filter"function of Ecmascript.

我想使用Ecmascript“过滤器”功能。

Suggestions ?

建议?

回答by Mihai Alexandru-Ionut

You can use somemethod and destructuring.

您可以使用somemethod 和destructuring

let roles = [ {roleId: "69801", role: "ADMIN"}, {roleId: "69806", role: "SUPER_ADMIN"}, {roleId: "69805", role: "RB"}, {roleId: "69804", role: "PILOTE"}, {roleId: "69808", role: "VENDEUR"}, {roleId: "69807", role: "SUPER_RB"} ]

const checkRoleExistence = roleParam => roles.some( ({role}) => role == roleParam)

console.log(checkRoleExistence("ADMIN"));
console.log(checkRoleExistence("RA"));
console.log(checkRoleExistence("RB"));

回答by Sujay

a little addition to all the answers given here. You can use find() to get value which matches your requirement.

对这里给出的所有答案的一点补充。您可以使用 find() 获取符合您要求的值。

const index = this.roles.findIndex(role=> role.name === 'ADMIN'); if (index >-1) { const value= this.roles[index].roleId); }

const index = this.roles.findIndex(role=> role.name === 'ADMIN'); if (index >-1) { const value= this.roles[index].roleId); }

this will give you roleId , where it matches your query

这会给你 roleId ,它匹配你的查询

回答by Lester

I got this solution for you: check this out

我为你找到了这个解决方案: 看看这个

export class RoleComponent implements OnInit {
  roles: Role[] = [];
  isRoleExist:boolean = false;
  constructor() { }

  ngOnInit() {
    const data = this.getRoles();
    this.roles = JSON.parse(data);

    this.isRoleExist = this.checkRoleExistence('PILOTE');
    console.log(this.isRoleExist);
  }

  checkRoleExistence(roleLabel: string):boolean {
    return this.roles.some(r => r.roleLabel === roleLabel);
  }

  getRoles() {
    return `[
    {"roleId": "69801", "roleLabel": "ADMIN"},
    {"roleId": "69806", "roleLabel": "SUPER_ADMIN"},
    {"roleId": "69805", "roleLabel": "RB"},
    {"roleId": "69804", "roleLabel": "PILOTE"},
    {"roleId": "69808", "roleLabel": "VENDEUR"},
    {"roleId": "69807", "roleLabel": "SUPER_RB"}
    ]`;
  }
}

export class Role {
  roleId: number;
  roleLabel: string;
}