Check if Object Has Key

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will be exploring how to check if a target value exists in a JSON object using JavaScript. We will be using the hasKey() function which allows us to sequentially check the keys in the object to determine if the target value is present. This lab will provide a better understanding of how to work with JSON objects in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28520{{"`Check if Object Has Key`"}} javascript/data_types -.-> lab-28520{{"`Check if Object Has Key`"}} javascript/arith_ops -.-> lab-28520{{"`Check if Object Has Key`"}} javascript/comp_ops -.-> lab-28520{{"`Check if Object Has Key`"}} javascript/cond_stmts -.-> lab-28520{{"`Check if Object Has Key`"}} javascript/array_methods -.-> lab-28520{{"`Check if Object Has Key`"}} javascript/destr_assign -.-> lab-28520{{"`Check if Object Has Key`"}} end

JavaScript Function to Check if Object Has Key

To check if a target value exists in a JavaScript object, use the hasKey function.

The function takes two arguments: obj, the JSON object to search in, and keys, an array of keys to check for. Here are the steps to check if the object has the given key(s):

  1. Check if the keys array is non-empty. If it is empty, return false.
  2. Use the Array.prototype.every() method to iterate over the keys array and sequentially check each key to internal depth of the obj.
  3. Use the Object.prototype.hasOwnProperty() method to check if obj does not have the current key or is not an object. If either of these conditions is true, stop propagation and return false.
  4. Otherwise, assign the key's value to obj to use on the next iteration.
  5. If the keys array has been iterated over successfully, return true.

Here's the code for the hasKey function:

const hasKey = (obj, keys) => {
  return (
    keys.length > 0 &&
    keys.every((key) => {
      if (typeof obj !== "object" || !obj.hasOwnProperty(key)) return false;
      obj = obj[key];
      return true;
    })
  );
};

Here are some examples of how to use the hasKey function:

let obj = {
  a: 1,
  b: { c: 4 },
  "b.d": 5
};

hasKey(obj, ["a"]); // true
hasKey(obj, ["b"]); // true
hasKey(obj, ["b", "c"]); // true
hasKey(obj, ["b.d"]); // true
hasKey(obj, ["d"]); // false
hasKey(obj, ["c"]); // false
hasKey(obj, ["b", "f"]); // false

Summary

Congratulations! You have completed the Check if Object Has Key lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like