Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Checking if object contains X in jQuery / ajax

How would I go about checking if a Object returned by a API contains X?

This is the closest I have gotten, without getting any errors:

$.ajax(settings).done(function (response) {

var email_response = {response};         

if (Object.values((JSON.stringify(email_response))).indexOf('"response"') > -1) {
            console.log('Is valid');
         
         else (console.log('Not valid'));
});

After running it results with the "Not valid" response from the if else function, even though the text "valid" is present in the return where "Is valid" would be the expected result.

Stringified using console.log(JSON.stringify(email_response)); this is what is returned from the API:

{"response":{"address":"test@domain.com","status":"valid","sub_status":"role_based_catch_all","free_email":false,"did_you_mean":null,"account":"test","domain":"domain.com","domain_age_days":"10116","smtp_provider":"","mx_found":"true","mx_record":"mx.domain.com","firstname":null,"lastname":null,"gender":null,"country":null,"region":null,"city":null,"zipcode":null,"processed_at":"2022-03-12 12:41:38.306"}}

I have tried to not stringify it as well, but this has the same result as explained above.

The following does also not see the specified text as present:

    Object.keys(email_response).forEach(function(key) {
    if ((email_response)[key] == 'valid') {
        console.log('Is valid');}
        else (console.log('Not valid'));
    
    });
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

you dont have to stringify if you want to get some data

$.ajax(settings).done(function (response) {

var status= response.status; // valid

//or 
console.log("status",response.status); // valid or undefined

 }); 

but if for some reasons you like to create a new object, use this code

var email_response = {"response":response}; 

var status= email_response.response.status; // valid
over 4 years ago · Santiago Trujillo Report

0

valid is not a key, it's a value. You need to check for the key status like this.

$.ajax(settings).done(function (response) {

    if (response.hasOwnProperty('status') && response.status === 'valid') {
        console.log('Is valid');
         
    } else {
        console.log('Not valid');
    }
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!