简体   繁体   English

在 object angular 6/typescript/javascript 的数组中查找

[英]finding within array of object angular 6/typescript/javascript

I am having below code in my HTML template of my angular 6 application.我的 angular 6 应用程序的 HTML 模板中有以下代码。

<div *ngIf ="conversation.participants[conversation.updatedBy.toLowerCase()].firstName">somedata </div>

basically I am trying to search within object of participants array by updatedBy value anad see its matching object has firstName property not null or not but it gives me error that firstName is not defined.基本上我正在尝试通过 updatedBy 值在参与者数组的 object 中搜索并查看其匹配的 object 具有 firstName 属性而不是 null ,但它给了我未定义 firstName 的错误。

I also tried placing question mark after ] brackets like below but did not yield any result.我还尝试在 ] 括号后放置问号,如下所示,但没有产生任何结果。

conversation.participants[conversation.updatedBy.toLowerCase()]?.firstName

Below is json object下面是 json object

"participants": [{
        "dateJoin": 1520409578,
        "dateLeft": 0,
        "firstName": "edh",
        "internalId": 165,
        "invitedBy": "edh",
        "lastName": "edh",
        "userId": "edh"
    }, {
        "dateJoin": 1520409578,
        "dateLeft": 0,
        "firstName": "",
        "internalId": 166,
        "invitedBy": "edh",
        "lastName": "",
        "userId": "ATB"
    }],
    "dataInAB": "ATB",
    "subject": "test ",
    "unreadMessageCount": 0,
    "updateDate": 1520585258,
    "updatedBy": "atb"
}

Please let me know if anything you know about this.如果您对此有所了解,请告诉我。

Thanks谢谢

You need to declare a variable in your component.ts file which will store the boolean result that you are using in *ngIf .您需要在 component.ts 文件中声明一个变量,该变量将存储您在*ngIf中使用的 boolean 结果。 And also update the value when the conversation variable changes.并在conversation变量更改时更新该值。

For example, in component.ts例如,在component.ts

let firstNameExists = false;

getConversationData(){
  this.conversation = ....
  this.firstNameExists  = this.conversation.participants.find(m=>m.userId.toLowerCase()==this.conversation.updatedBy).firstName? true: false;
}

and then in your component.html然后在你的组件中。html

<div *ngIf ="firstNameExists">somedata </div>

Note: the expression is being used in ts because it's a very bad practice to use expressions in angular bindings.注意:在 ts 中使用了表达式,因为在 angular 绑定中使用表达式是一种非常糟糕的做法。

this.conversation.participants.find(x => x.userId.toLowerCase() === this.conversation.updatedBy).firstName;

stackblitz 堆栈闪电战

try this尝试这个

your html你的 html

  <div *ngIf="isVisible"> some data </div>

your component.ts你的 component.ts

  public isVisible = false;


  // inside your get method
  this.conversation = { your obj with participants array };
  this.isVisible = (this.conversation.participants.filter(p => 
    (p.firstName.toLowerCase()) === this.conversation.updatedBy).length) ? true : false;

if you are looping it inside *ngFor , then you can do this如果你在*ngFor里面循环它,那么你可以这样做

your html你的 html

<div *ngIf="getVisibility(participant)"> some data </div>

your component.ts你的 component.ts

getVisibility(participant) {
  return (this.conversation.updatedBy.toLowerCase() === participant.firstName.toLowerCase) ? true : false;
}

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

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