2017-07-27 12 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 
        } 
       ] 
      } 
     ] 

}] 
} 

は私が動的にその値が「子供」のnullで、変数にこれらのオブジェクトの「名前」を記憶するオブジェクトにアクセスしたいです。例えば、この場合、森林面積またはプランテーション。どうすればjavascriptやjqueryを使ってこれを行うことができますか?

+0

あなたは*動的*アクセスによって何を意味するかについて詳しく説明することはできますか?ネストレベルに関係なく意味しますか? – Taplar

答えて

0

あなたはそれが何よりも速いですが、シンプルなforを行うと、最も可能性が高いだろう、このためのjQueryを必要としません:

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)を提供する必要があります。そして、副注釈として、それは 'のためのものよりもずっと遅いです。 –

0

は、再帰的配列を反復とchildren = nullを探し、すべてのオブジェクトnamesarrayを与えます。

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

解決策が静的でなく、動的で、 'children = null'のすべてのオブジェクトが配列に含まれることを示すために、' children is null'という 'landuse'という名前のオブジェクトをもう1つ追加しました –

関連する問題