10

我有一个 JSON 输入,可以进入任意数量的级别。

我给了一个输入样本

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"
                    }
              ]
        }
  ]

}});

我想使用递归将所有级别的键值“实体”​​添加到数组中,

我可以使用字符串从第一级收集数据

<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>

如何使用递归函数进入 JSON 字符串的内部级别?

4

5 回答 5

22

我制作了一个jsfiddle,它像这样遍历 JS 对象中的每个对象、数组和值...

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
    };

};

我没有得到递归错误(在 Chrome 中)。你能用它来做你想做的事吗?

如果您需要测试对象是否为数组,请使用if (obj instanceof Array)

要测试对象是否具有“实体”属性,请使用if (obj.hasOwnProperty('entity'))

要添加(或修改现有)“实体”属性,请使用obj.entity = valueobj['entity'] = value

于 2012-05-05T08:18:56.687 回答
1
(function recur( obj ) {
    Object.keys( obj ).forEach( function( prop ) {
        // Check if the property is an object
        if ( ({}).toString.apply( prop ) === '[object Object]' ) {
            // If it is, recall this function
            recur( prop );
        }
    } );
} () );

我没有添加你的逻辑,但你知道如何递归地遍历你的对象。

于 2012-05-05T07:49:45.537 回答
1

这是我经常使用的一个功能。执行许多递归任务很容易修改。例如,如果您添加一个保释标志,您可以快速获取堆栈或添加一个回调函数,使其更加通用。无论如何,这是我的 2 美分

var recursiveObjMap = (function(){
  var stack = [];
  var result = [];
  // var bail = false;
  return function map(data, key){
    if (!$.isArray(data) && !$.isPlainObject(data) ) { 
      result.push(data);
      return false 
    }

    $.each(data, function(i, v){
      if (key) stack.push(key);
      map(v, i);
      stack.pop();
    });
    return result;
  };
})();

recursiveObjMap({a:'b',c:{d:{e:"f"}}}) // ['b', 'f']
于 2016-10-06T06:41:35.363 回答
1

假设我有如下结构:

var aObject = {
    items: [],
    children: {}
}

Children 是一个包含更多 aObject 的关联数组。所以它可能看起来像这样:

var aObject = {
    items: [],
    children: {
        "subgroup1": {
            items: [],
            children: {}
        },
        "subgroup2": {
            items: [],
            children: {}
        }
    }
}

我有一个包含子组数组的项目:

["subgroup1", "subgroup1a"]

每个子组都是一个“位置”。该项目需要放置在:

aObject.children[array[0]].children[array[1]].items

在每个级别,我们必须检查 children[array[i]] 是否存在,如果不存在,则创建它。你不能简单地写 aObject.children[array[0]].children[array[1]].items.push(item) 因为 children[array[0]] 可能不存在,我们会得到一个错误。

这可以使用递归来解决!(AngularJS)

function recursive(aLevel, aItem, aArray, aIndex){
    var lLevel = aLevel;

    // If we have reached the end of the array
    if (aIndex === aArray.length){
        // Insert
        aLevel.items.push(aItem);
    } else {

        // If the subgroup doesn't exist, create it
        if (typeof aLevel.children[aArray[aIndex]] === 'undefined'){
            aLevel.children[aArray[aIndex]] = {
              items: [],
              children: {}
            };
        }

        // Move into
        recursive(aLevel.children[aArray[aIndex]], aItem, aArray, aIndex+1);
    }
}

aObject = {
    items: [],
    children: {},
}

angular.forEach(items, function(item, i){
    var location = item.location;

    if (location.length == 0){
        aObject.items.push(item);
    } else {
        recursive(aObject, item, location, 0);
    }
});

最终的 aObject 如下所示:

var aObject = {
     items: [],
     children: {
        "subgroup1": {
            items: [],
            children: {
                "subgroup1a": {
                    items: [item],
                    children: {}
                }
            }
        },
        "subgroup2": {
            items: [],
            children: {}
        }
    }
}
于 2016-04-19T01:26:21.720 回答
0
const obj = [
  {
    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 test(runObj) {
  for (let i in runObj) {
    typeof runObj[i] == "object" ? test(runObj[i]) : console.log(runObj[i]);
  }
}
test(obj);
于 2020-10-30T00:34:19.743 回答