Skip to content

How to get key and value from JSON array object in JavaScript | Example code

  • by

You can use the map() function or loop over them to either get the key and value from the JSON array object in JavaScript.

Get key and value from JSON array object in JavaScript

Simple example code using a map.

<!DOCTYPE html>
<html>
<head>

  <script>

    let arr = [
    {name: "AAA", age: 10},
    {name: "BBB", age: 20},
    {name: "CCC", age: 30}
    ]

    let res = arr.map(function(elem){ 
      return elem.name +"  "+ elem.age; }).join(",");

    console.log(res)

  </script>

</head>
</html>

Output:

How to get key and value from JSON array object in JavaScript

Using loop

JSON content is basically represented as an associative array in JavaScript. Just use for-loop over them to either read the key or the value.

  <script>

    var JSON_Obj = { "one":1, "two":2, "three":3, "four":4, "five":5 };

    // Read key
    for (var key in JSON_Obj) {
     console.log(key);
     console.log(JSON_Obj[key]);
   }

 </script>

Output:

get the key value in a JSON object

Do comment if you have any doubts or suggestions on this JS array 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 *