0

I have a JSON input of

{
    "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 need to get the values for entity in each level in to an array..

[Company, Ford Motor Co, Ford, ....... , Sterling Heights]

I am able to get thru the first level with the code

for (var k in h.Categories.Facets)
{

alert(h.Categories.Facets[k].entity);

}

How do I reach thru the innerlevels to get the values of entity??

4 Answers 4

1

You should do a foreach on each entity. If you know how many levels there are you can nest loops. If not - probably should go for a recursive function.

Edit

Recursive function:

function getEntities(ent)
{
   alert(ent);
   for (var l in ent)
   {
      getEntities(ent[l].entity);
   }
}

Then use:

for (var k in h.Categories.Facets)
{
   getEntities(h.Categories.Facets[k]);
}

Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

Im not familiar with recursive functions.. The levels will be different for each input, so I guess there is no other option other than recursive..
1

The most general recursive answer:

function getEntities(any) {
    var entities = [];
    if ('entity' in any​) {
        entities.push(any.entity);
    }
    for (var prop in any) {
        if (any[prop] instanceof Object && any.hasOwnProperty(prop)) {
            entities.append(getEntities(any[prop]));
        }
    }
    return entities;
}
console.log(getEntities(h));

The line:

 if (any[prop] instanceof Object && any.hasOwnProperty(prop)) { 

Keeps numbers/nulls from bombing and the any.hasOwnProperty(prop) makes up for frameworks that like to attach to the Object prototype.

Comments

0

One way is to write a recursive method that accepts an array and extracts the methods from it.

Comments

0

As suggested, you can use a recursive function to loop through every possible nested combo:

var allEntities = [];
function getEntities(obj)
{
   if (obj != null)
   {
       var entityName = obj["entity"];
       alert(entityName);
       if (entityName != null)
       {
           allEntities.push(entityName);
           var adjName = entityName.replace(/ /gi, "_");
           var entities = obj[adjName];
           if (entities != null)
           {
               for (var e in entities)
               {
                   var innerEntities = getEntities(entities[e]);
                   for (var inner in innerEntities)
                       allEntities.push(innerEntities[inner]);
               }
           }
       }
    }
}    

for (var k in h.Categories.Facets)
{
    alert(h.Categories.Facets[k].entity);
    getEntities(h.Categories.Facets[k]);
}
alert(allEntities.length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.