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

178
Vistas
How to check whether a specific string occurs as a value in an object?

I have an object that is structured like this (simplified version):

{
  "customConfig": {
    "homepage": {
      "url": "path/to/homepage",
      "base_path": "www.stackoverflow.com"
    },
    "portfolio": {
      "url": "path/to/portfolio",
      "base_path": "www.github.com"
    },
    "moreStuff": {
        //...
    }
}

Now I want to check whether a specific string value (in my case for the key base_path) exists.

So how can I check whether, for example, the string www.github.com exists in my customConfig object for the key base_path?

A boolean return value would be enough here.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The OP needs to utilize Object.values in order to retrieve an array of all the config object's property values. Then the OP wants to know if some condition like such a value's/item's base_path value equals a certain address.

console.log(
  "does object contain an item where `base_path` equals 'www.github.com' ?",
  Object.values({
      "homepage": {
        "url": "path/to/homepage",
        "base_path": "www.stackoverflow.com"
      },
      "portfolio": {
        "url": "path/to/portfolio",
        "base_path": "www.github.com"
      },
      "moreStuff": {
          //...
      }
    }).some(item => item.base_path === 'www.github.com')
);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use a recursive function.

To find a certain key, call hasOwnProperty (or use includes on Object.keys) at each nesting level to see if the key is found there.

To find a certain value, use includes on Object.values().

To find a key/value pair, find the key (as above) and verify that the value for that key is the searched value:

const containsKey = (obj, key) => Object(obj) === obj && (
    obj.hasOwnProperty(key) ||
    Object.values(obj).some(child => containsKey(child, key))
); 

const containsValue = (obj, value) => Object(obj) === obj && ( 
    Object.values(obj).includes(value) ||
    Object.values(obj).some(child => containsValue(child, value))
); 

const containsKeyValue = (obj, key, value) => Object(obj) === obj && (
    obj.hasOwnProperty(key) && obj[key] === value ||
    Object.values(obj).some(child => containsKeyValue(child, key, value))
); 

// Example run:
let data = {
  "customConfig": {
    "homepage": {
      "url": "path/to/homepage",
      "base_path": "www.stackoverflow.com"
    },
    "portfolio": {
      "url": "path/to/portfolio",
      "base_path": "www.github.com"
    },
    "moreStuff": {
        //...
    }
  }
};

console.log(containsKey(data, "base_path")); // true
console.log(containsValue(data, "www.github.com")); // true
console.log(containsKeyValue(data, "base_path", "www.github.com")); // true

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try something like

let obj = {
  "customConfig": {
    "homepage": {
      "url": "path/to/homepage",
      "base_path": "www.stackoverflow.com"
    },
    "portfolio": {
      "url": "path/to/portfolio",
      "base_path": "www.github.com"
    }
    }
}

let exist = Object.keys(obj.customConfig).filter(page => obj.customConfig[page]['base_path'] == 'www.github.com').length>0;

console.log(exist);

about 4 years ago · Juan Pablo Isaza 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