Skip to content

JavaScript compares object key values | Code

  • by

First, get keys from the object as an array using the object keys() method and then compares object key values to the given key in the loop in JavaScript.

JavaScript compares object key values

Simple example code.

<!DOCTYPE html>
<html>
<body>

  <script>
   var myString = "Item1";

   var jsObject = 
   {
    Item1:
    {
      "apples": "red",
      "oranges": "orange",
    },
    Item2:
    {
      "bananas": "yellow",
      "pears": "green"
    }
  };

  var keys = Object.keys(jsObject); 

  keys.forEach(function(key) { 
    console.log(key, key == myString)
  });
</script>

</body>
</html>

Output:

JavaScript compares object key values

Do comment if you have any doubts or suggestions on this Js object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *