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

169
Views
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 answers
Answer question

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 Report

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 Report

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 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!