0

I have list of objects response.tasks: {userLastName: ', srCreationDate: '', idNumber: '', firstName: ', insStartDate: null, …} and I want to know if I have some properties for userLastName it will be true for 'example' will be false. I need to write this condition via html

I tried 'userLastName' in response.tasks

response.tasks.hasOwnProperty('userLastName')

response.tasks.includes('userLastName')`

but all of them were false

  • See if this helps- https://stackoverflow.com/questions/455338/how-do-i-check-if-an-object-has-a-key-in-javascript – Rajeev Dec 20 '22 at 19:23

2 Answers2

2

You have an array (multiple items) so you need to check if "some" item has the property

response.tasks.some(task => task.hasOwnProperty('userLastName'))

or "every" item has the property

response.tasks.every(task => task.hasOwnProperty('userLastName'))

depending on your requirements

Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
  • Thank you so much! do you know how can I do it in html`
    task.hasOwnProperty('userLastName'))" mat-sort-header="srCreationDate">
    ` because I am using `` to see the data
    – Uri Perkal Dec 20 '22 at 19:26
  • @UriPerkal This is angular not html, right? I am not an expert in angular, but it seems that something like this should do the trick. Though I suggest you to put this logic somewhere else in your component code to keep your template as simple as possible. – Yury Tarabanko Dec 20 '22 at 19:52
0

You can use in operator.

The in operator returns true if the specified property is in the specified object or its prototype chain.

Example:

interface I1 {
    userLastName: string
}


let myVar: I1 = {
    userLastName: 'test'
}

console.log('userLastName' in myVar); // returns true
tmsbrndz
  • 1,297
  • 2
  • 9
  • 23