-1

My object data is as follows:

var object = '({10:{id:"10", v_title:"1", ' +
              '13:{id:"13", v_title:"1.1", ' +
                '15:{id:"15", v_title:"1.1.1 ", v_noofpara:"g2 ", v_description:"d1.1.1"}' +
              '}'+
           '},'+ 
           '11:{id:"11", v_title:"2", ' +
             '14:{id:"14", v_title:"2.1", ' +
               '16:{id:"16", v_title:"2.1.1 ", v_noofpara:"g1 ", v_description:"des2.1.1 "}' +
             '}' +
           '}' +
         '})'

Note that the key value 10(Grand Parent) has 13(parent) and 13 has 15(child). Similarly 11(parent) has 14 and 14 has 16.

Using hasOwnProperty or any other methods can any one give me the count with relationship so that use it further.

Lalit Sharma
  • 555
  • 3
  • 12
  • What have you tried? And you don't have an object, you have a JSON string. If you wish to traverse it you'll first need to parse it. – Mitya Jul 04 '12 at 11:14
  • 1
    That's a string and it cannot even be parsed as valid JSON. You'd have to run `eval` on it. – Esailija Jul 04 '12 at 11:14
  • Can you please give an example of your desired output? I don't understand what you're asking. – nnnnnn Jul 04 '12 at 11:15

2 Answers2

1

Since you are not dealing with a valid JSON structure, you have to run it through eval:

var myObject = eval(object);

Now you have a valid Javascript Object that you can loop through to get the count of each child or the sum of each first child etc. as you please.
Refer to this StackOverflow question about how to get the length of an object. Answers with and without jQuery are provided.

Community
  • 1
  • 1
0

You can use eval to convert it to an object:

var o = eval(object);

Now you can use for..in to iterate over the properties. You can test the value of each property and if it's an object, do for..in on it too, ad infinitum.

RobG
  • 142,382
  • 31
  • 172
  • 209