2017-07-27 33 views
1

我有以下的JSON:如何通過動態遍歷JSON數據將對象存儲在JSON數據中的某個級別?

{ 
"menu": [{ 

     "name": "vegetation", 
     "id": "1", 
     "children": [ 
      { 
       "name": "landuse", 
       "id": "1.1", 
       "children": [ 
        { 
         "name": "forest area", 
         "id": "1.1.1", 
         "children": null 
        }, 
        { 
         "name": "plantation", 
         "id": "1.1.2", 
         "children": null 
        } 
       ] 
      } 
     ] 

}] 
} 

我要動態訪問的「孩子」的,其值是空的對象,並存儲在一個變量這些對象的「名字」。例如在這種情況下,無論是森林面積還是種植園。我如何使用JavaScript或jQuery來做到這一點?

+0

你能否解釋一下你的意思是*動態訪問什麼*?你的意思是不管嵌套級別? – Taplar

回答

0

你不需要jQuery的這一點,一個簡單的for會做,而最有可能的,這比什麼都更快:

var childless = [], 
 
    checkForChildren = function(items){ 
 
     for (var i = 0; i < items.length; i++) { 
 
     if (items[i].children) 
 
      checkForChildren(items[i].children); 
 
     else 
 
      childless.push(items[i]); 
 
     } 
 
    }; 
 
    
 
// test it: 
 
var menu = [{ 
 
    "name": "vegetation", 
 
    "id": "1", 
 
    "children": [{ 
 
     "name": "landuse", 
 
     "id": "1.1", 
 
     "children": [{ 
 
     "name": "forest area", 
 
     "id": "1.1.1", 
 
     "children": null 
 
     },{ 
 
     "name": "plantation", 
 
     "id": "1.1.2", 
 
     "children": null 
 
     }] 
 
    }] 
 
    }]; 
 

 
checkForChildren(menu); 
 
console.log(childless);

0

遞歸想到的。

var childless = []; 
var recursive_function = function(obj){ 
    if(obj.children == null){ 
    childless.push(obj); 
    } else { 
    $.each(obj.children, function(child){ 
     recursive_function(child); 
    } 
    } 
}; 
$.each(json_obj.menu, function(root_level){ 
    recursive_function(root_level); 
}); 
console.log(childless); 
console.log($.map(childless, function(x){return x.name;})); 
0
var test = { 
 
    "menu": [{ 
 

 
    "name": "vegetation", 
 
    "id": "1", 
 
    "children": [{ 
 
     "name": "landuse", 
 
     "id": "1.1", 
 
     "children": [{ 
 
      "name": "forest area", 
 
      "id": "1.1.1", 
 
      "children": null 
 
     }, 
 
     { 
 
      "name": "plantation", 
 
      "id": "1.1.2", 
 
      "children": null 
 
     } 
 
     ] 
 
    }] 
 

 
    }] 
 
}; 
 

 
var hasNullChildren = []; 
 

 
function checkChildren (children) { 
 
    //loop over all children 
 
    children.forEach(function(child){ 
 
    //if no children, add name to list 
 
    if (!child.children) hasNullChildren.push(child.name); 
 
    //check nested children 
 
    else checkChildren(child.children); 
 
    }); 
 
} 
 

 
//start the recursion loop 
 
checkChildren(test.menu); 
 
console.log(hasNullChildren);

+0

請注意'Array.prototype.forEach()'在當前所有瀏覽器中都不受支持。你應該爲沒有實現的瀏覽器提供一個[Polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill)。而且,作爲一個方面說明,它比'for'慢得多。 –

0

通過陣列遞歸迭代並搜索children = null,給人的array與對象的所有names

const obj = { 
 
"menu": [{ 
 

 
     "name": "vegetation", 
 
     "id": "1", 
 
     "children": [ 
 
      { 
 
       "name": "landuse", 
 
       "id": "1.1", 
 
       "children": [ 
 
        { 
 
         "name": "forest area", 
 
         "id": "1.1.1", 
 
         "children": null 
 
        }, 
 
        { 
 
         "name": "plantation", 
 
         "id": "1.1.2", 
 
         "children": null 
 
        } 
 
       ] 
 
      },{ 
 
       "name": "landuse", 
 
       "id": "1.1", 
 
       "children": null 
 
      } 
 
     ] 
 

 
}] 
 
} 
 

 
function getNameWithNullChildren(arr) { 
 
    let array = []; 
 
    arr.forEach(item => { 
 
    if(item.children === null) { 
 
     array.push(item.name); 
 
    } else { 
 
     array = getNameWithNullChildren(item.children); 
 
    } 
 
    }); 
 
    return array; 
 
} 
 

 
console.log(getNameWithNullChildren(obj.menu));

+0

您的解決方案也打印出Landuse。你能弄清楚爲什麼? – Salman

+0

我已經添加了一個名爲'name as landuse'的'children'爲null'的對象,只是爲了表明解決方案不是靜態的,它是動態的,並且具有'children = null'的所有對象都將包含在數組中 –