0

below is the response I am getting from my back end call.

 {     name: 'abc',
       id : '1234',
       emp: 'abc@sample.com'
    }

In my UI i want to test if the emp === abc@sample.com exist then perform the certain operation, if not show the error message as emp don't exist. If the response is an Array, it would be easy to search for employee. But I am not sure, how to do it for object response.Can anyone help me with this

Singh
  • 207
  • 1
  • 5
  • 14

1 Answers1

1

Check with . operator on Object such as

response.emp returns value of emp property in response object.

This will work!

var response = {
  name: 'abc',
  id: '1234',
  emp: 'abc@sample.com'
}

if (!response.emp) throw new Error('emp does not exist!')

if (!response.emp === 'abc @sample.com') throw new Error('emp "abc @sample.com" does not exist!')
Radical Edward
  • 2,824
  • 1
  • 10
  • 23