0

I am learning about javascript and json objects and arrays. I have been given task of iterating following array:

{"6784":
    {"OD":
        [
            {
                "od_id":"587641",
                "cl_type":"scl",
                "cl_eye":"OD"
            }
        ],
    }
    {"OS":
        [
            {
                "od_id":"587641",
                "cl_type":"scl",
                "cl_eye":"OD"
            }
        ],
    }
}

I have tried to iterate with simple for loop and jquery each, but it is not working. The condition is that we do not know any key name.

Vishal Suri
  • 447
  • 2
  • 12
  • 32

5 Answers5

1

If you don't know the key names,you can use Object.keys(json_obj).

Object.keys(json_obj).forEach(function(key){
   stuff
}
bigbounty
  • 16,526
  • 5
  • 37
  • 65
1

Your JSON is not a valid JSON.enter image description here

valid JSON should be like : enter image description here

Working Demo

var jsonObj = {
 "6784": {
  "OD": [{
   "od_id": "587640",
   "cl_type": "scl",
   "cl_eye": "OD"
  }],
  "OS": [{
   "od_id": "587641",
   "cl_type": "scl",
   "cl_eye": "OD"
  }]
 }
};

var keys = Object.keys(jsonObj);
for (var i in keys) {
  var innerKeys = Object.keys(jsonObj[keys[i]]);
  for (var j in innerKeys) {
    console.log(jsonObj[keys[i]][innerKeys[j]][0].od_id);
  }
}
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

You can use:

Object.keys(Obj)

to identify properties on your object and then can iterate over it to find values of each property.

0

{} denotes an object, not an array, and you can iterate over its properties in multiple ways:

Iterate through object properties

You might also want to have a look at:

looping through an object (tree) recursively

j4nw
  • 2,227
  • 11
  • 26
0
**index.html**

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>


</head>
<body>

<div id="content"></div>



<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>

<script src="js/main.js"></script>

</body>
</html>



**js/main.js**

$(document).ready(function(){

console.log(data)

function parse(data){

for (var item in data['6784']) {
  for (var i = 0; i < data['6784'][item].length; i++) {
      var od_id = data['6784'][item][i].od_id;
      var cl_type = data['6784'][item][i].cl_type;
      var cl_eye = data['6784'][item][i].cl_eye;

      console.log(item + ', ' + od_id + ', ' + cl_type + ', ' + cl_eye);

      $("#content").append(
        '<div class="row">'+  
        '<p><span class="item">item: </span> ' + item + '</p>'+
        '<p><span class="item">od_id: </span> ' + od_id + '</p>'+
        '<p><span class="item">cl_type: </span> ' + cl_type + '</p>'+
        '<p><span class="item">cl_eye: </span> ' + cl_eye + '</p>'+
        '</div> <br>' 
      );

    }
  }

}

$.ajax({
 url: 'data.json', 
 dataType: "json",
 success: parse,

});
});





**data.json**

{"6784":
    {"OD":
        [
            {
                "od_id":"587641",
                "cl_type":"scl",
                "cl_eye":"OD"
            }
        ],
    "OS":
        [
            {
                "od_id":"58764-2",
                "cl_type":"scl-2",
                "cl_eye":"OS"
            }
        ]
    }
}
memre
  • 11