Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
javascript - validate nested key inside an object

I have the following JavaScript object.

let obj = {
  "type": "user",
  "personalDetails": {
    "firstName": "Steven",
    "address": {
      "streetName": "abcd",
      "zipCode": "012345"
    }
  },
  "employeeDetails": {
    "designation": {
      "id": "1234",
      "manage": "Mark",
    }
  }
}

How can I create a dynamic function which will take this JS object obj as a first argument, one parent node as the second argument and a child node as the third argument.

if the third argument is present as a child node inside the second argument, the function should return true. For example.

const childNodeExist = (obj, personalDetails, firstName) => {
    //.. since firstName exists in personalDetails should return true
}

const childNodeExist = (obj, employeeDetails, designation) => {
    //.. since designation exists in employeeDetails should return true
}
const childNodeExist = (obj, employeeDetails, salary) => {
    //.. since salary exists in employeeDetails should return false
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can try using Object.prototype.hasOwnProperty():

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

let obj = {
  "type": "user",
  "personalDetails": {
    "firstName": "Steven",
    "address": {
      "streetName": "abcd",
      "zipCode": "012345"
    }
  },
  "employeeDetails": {
    "designation": {
      "id": "1234",
      "manage": "Mark",
    }
  }
}

const childNodeExist = (obj, prop, childProp) => {
    return obj[prop]?.hasOwnProperty(childProp) ?? false; 
}
console.log(childNodeExist(obj, 'personalDetails', 'firstName'));

console.log(childNodeExist(obj, 'employeeDetails', 'designation'));

console.log(childNodeExist(obj, 'employeeDetails', 'salary'));

Update: Alternative way to support in all the browsers:

let obj = {
  "type": "user",
  "personalDetails": {
    "firstName": "Steven",
    "address": {
      "streetName": "abcd",
      "zipCode": "012345"
    }
  },
  "employeeDetails": {
    "designation": {
      "id": "1234",
      "manage": "Mark",
    }
  }
}

const childNodeExist = (obj, prop, childProp) => {
    return obj.hasOwnProperty(prop)? obj[prop].hasOwnProperty(childProp) : false; 
}
console.log(childNodeExist(obj, 'personalDetails', 'firstName'));

console.log(childNodeExist(obj, 'employeeDetails', 'designation'));

console.log(childNodeExist(obj, 'employeeDetails', 'salary'));

about 4 years ago · Juan Pablo Isaza Relatório

0

Is this what you require?

const childNodeExist = (obj, parentNode, childNode) => {
    return (obj[parentNode][childNode] ? true : false);
}

let obj = {
  "type": "user",
  "personalDetails": {
    "firstName": "Steven",
    "address": {
      "streetName": "abcd",
      "zipCode": "012345"
    }
  },
  "employeeDetails": {
    "designation": {
      "id": "1234",
      "manage": "Mark",
    }
  }
}

console.log(childNodeExist(obj, "personalDetails", "firstName"));
console.log(childNodeExist(obj, "employeeDetails", "designation"));
console.log(childNodeExist(obj, "employeeDetails", "salary"));

about 4 years ago · Juan Pablo Isaza Relatório

0

This would do the trick:

  const childNodeExist = (obj, parentNode, childNode) => Boolean(obj[parentNode][childNode])
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda