Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  9] [ 4]  / answers: 1 / hits: 40349  / 12 Years ago, sat, may 5, 2012, 12:00:00

I have a JSON input which can go to any number of levels.



I'm giving an input sample of



var d=getEntities( {Categories: 
{
Facets:
[
{
count: 1,
entity: Company,
Company:
[
{

entity: Ford Motor Co,

Ford_Motor_Co:
[
{
count: 1,
entity: Ford
}
]
}
]
},
{
count: 4,
entity: Country,
Country: [
{

entity: Germany,
Germany: [
{
count: 1,
entity: Germany
}
],
currency: Euro (EUR)
},
{

entity: Italy,
Italy: [
{
count: 1,
entity: Italy
}
],
currency: Euro (EUR)
},
{

entity: Japan,
Japan: [
{
count: 1,
entity: Japan
}
],
currency: Yen (JPY)
},
{

entity: South Korea,
South_Korea: [
{
count: 1,
entity: South Korea
}
],
currency: Won (KRW)
}
]
},
{count: 5,
entity: Persons,
Persons: [
{
count: 2,
entity: Dodge
},
{
count: 1,
entity: Dodge Avenger
},
{
count: 1,
entity: Major League
},
{
count: 1,
entity: Sterling Heights
}
]
}
]

}});


I want to add the key value Entity in all levels to an array using recursion,



I'm able to collect the data from first level using the string



<html>
<head>
<script src=jquery.js type=text/javascript></script>
<script type=text/javascript src=dataDumper.js></script>


<script type=text/javascript>

var testJSON = {Categories:
{
Facets:
[
{
count: 1,
entity: Company,
Company:
[
{

entity: Ford Motor Co,

Ford_Motor_Co:
[
{
count: 1,
entity: Ford
}
]
}
]
},
{
count: 4,
entity: Country,
Country: [
{

entity: Germany,
Germany: [
{
count: 1,
entity: Germany
}
],
currency: Euro (EUR)
},
{

entity: Italy,
Italy: [
{
count: 1,
entity: Italy
}
],
currency: Euro (EUR)
},
{

entity: Japan,
Japan: [
{
count: 1,
entity: Japan
}
],
currency: Yen (JPY)
},
{

entity: South Korea,
South_Korea: [
{
count: 1,
entity: South Korea
}
],
currency: Won (KRW)
}
]
},
{count: 5,
entity: Persons,
Persons: [
{
count: 2,
entity: Dodge
},
{
count: 1,
entity: Dodge Avenger
},
{
count: 1,
entity: Major League
},
{
count: 1,
entity: Sterling Heights
}
]
}
]

}};

function scan(obj)
{
var k;
if (obj.hasOwnProperty('entity')) {



for (k in obj){
if (obj.hasOwnProperty(k)){


scan( obj[k] );


}
}
}


else{
if(k=='entity')
{
alert(obj.entity);
}
}


};

scan(testJSON);



</script>
</head>

<body>

</body>

</html>


How do I get in to the inner levels for JSON string using recursive functions?


More From » arrays

 Answers
26

I have made a jsfiddle which traverses every object,array and value in the JS object like so...



function scan(obj) {
var k;
if (obj instanceof Object) {
for (k in obj){
if (obj.hasOwnProperty(k)){
//recursive call to scan property
scan( obj[k] );
}
}
} else {
//obj is not an instance of Object so obj here is a value
};

};


I get no recursion error (in Chrome). Can you use this to do what you want?



If you need to test if an object is an array use if (obj instanceof Array)



To test if an object has an entity property use if (obj.hasOwnProperty('entity'))



To add (or modify an existing) entity property use obj.entity = value or obj['entity'] = value


[#85780] Thursday, May 3, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlynd

Total Points: 470
Total Questions: 108
Total Answers: 120

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;