Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda