0

I am newbie to JavaScript.I have a problem in display objects within objects in my example shown below.I am not able to display the objects content.

var nameList={
   n1:{
       name:"PRS POP",
       section:211,
       rollNo:211,
      },
   n2:{
    name:"steve XYZ",
    section:32,
    rollNo:359,
      }
 }

function display_proc()
 {
   var x=0;
   var objProp;
   for(objProp in nameList){
    if(nameList.hasOwnProperty(objProp)){
        for(var obj in nameList.objProp ){
            if(nameList.objProp.hasOwnProperty(obj)){
                document.writeln(obj);
              }
           }
        }
      }
    }
display_proc();
Pranav C Balan
  • 106,305
  • 21
  • 136
  • 157
  • 1
    [It's `[objProp]` not `.objProp`](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – Bergi May 10 '16 at 06:08
  • 1
    `if(nameList.hasOwnProperty(objProp)){` is unnecessary. You should omit it for simplicity. – Bergi May 10 '16 at 06:09
  • 1
    You [should stop using `document.writeLn`](http://stackoverflow.com/q/802854/1048572) immediately. – Bergi May 10 '16 at 06:10
  • Bellow link contains code for read all properties of objects [http://stackoverflow.com/questions/36846682/iterate-object-with-loop-between-different-data-options/36847028#36847028](http://stackoverflow.com/questions/36846682/iterate-object-with-loop-between-different-data-options/36847028#36847028) – Vijay Thorat May 10 '16 at 06:15

1 Answers1

1

You are trying to get objProp property of the object, which is not defined at all. So it will not work at all, instead you need to get the property by using string variable for that use square bracket notation.

var nameList = {
  n1: {
    name: "PRS POP",
    section: 211,
    rollNo: 211,
  },
  n2: {
    name: "steve XYZ",
    section: 32,
    rollNo: 359,
  }
}

function display_proc() {
  var x = 0;
  var objProp;
  for (var objProp in nameList) {
    if (nameList.hasOwnProperty(objProp)) {
      for (var obj in nameList[objProp]) {
        if (nameList[objProp].hasOwnProperty(obj)) {
          document.writeln(nameList[objProp][obj]);
        }
      }
    }
  }
}
display_proc();
Pranav C Balan
  • 106,305
  • 21
  • 136
  • 157