JQuery使用父子关系嵌套每个

时间:2016-06-29 06:13:36

标签: jquery json

我的json系列是

    var ds=[

{"ParentID":39,"Value":1,"Text":"LM7","Selected":false},
{"ParentID":41,"Value":2,"Text":"ud5","Selected":false},
{"ParentID":null,"Value":3,"Text":"H1a","Selected":false},
{"ParentID":40,"Value":4,"Text":"jv/","Selected":false},
{"ParentID":40,"Value":5,"Text":"axr","Selected":false},
{"ParentID":24,"Value":6,"Text":"Fay","Selected":false},
{"ParentID":24,"Value":7,"Text":"SFt","Selected":false},
{"ParentID":25,"Value":9,"Text":"3e5","Selected":false},
{"ParentID":24,"Value":11,"Text":"J7m","Selected":false},
{"ParentID":24,"Value":12,"Text":"LYD","Selected":false},
{"ParentID":24,"Value":13,"Text":"+Xt","Selected":false},
{"ParentID":null,"Value":14,"Text":"w03","Selected":false},
{"ParentID":null,"Value":15,"Text":"w03","Selected":false},
{"ParentID":null,"Value":16,"Text":"w03","Selected":false},
{"ParentID":null,"Value":17,"Text":"SFt","Selected":false},
{"ParentID":null,"Value":18,"Text":"XlG","Selected":false},
{"ParentID":null,"Value":19,"Text":"w03","Selected":false},
{"ParentID":null,"Value":20,"Text":"rd7","Selected":false},
{"ParentID":null,"Value":21,"Text":"BJf","Selected":false},
{"ParentID":null,"Value":22,"Text":"jIl","Selected":false},
{"ParentID":null,"Value":23,"Text":"Nws","Selected":false},
{"ParentID":null,"Value":24,"Text":"bIQ","Selected":false},
{"ParentID":null,"Value":25,"Text":"3e5","Selected":false},
{"ParentID":null,"Value":26,"Text":"DTo","Selected":false},
{"ParentID":null,"Value":27,"Text":"g/F","Selected":false},
{"ParentID":null,"Value":28,"Text":"J7m","Selected":false},
{"ParentID":null,"Value":29,"Text":"enF","Selected":false},
{"ParentID":null,"Value":30,"Text":"enF","Selected":false},
{"ParentID":null,"Value":31,"Text":"+Xt","Selected":false},
{"ParentID":null,"Value":32,"Text":"l9g","Selected":false},
{"ParentID":null,"Value":33,"Text":"7Ct","Selected":false},
{"ParentID":null,"Value":34,"Text":"LYD","Selected":false},
{"ParentID":null,"Value":35,"Text":"7D2","Selected":false},
{"ParentID":null,"Value":36,"Text":"7D2","Selected":false},
{"ParentID":null,"Value":37,"Text":"1+/","Selected":false},
{"ParentID":37,"Value":38,"Text":"cIF","Selected":false},
{"ParentID":null,"Value":39,"Text":"D6a","Selected":false},
{"ParentID":null,"Value":40,"Text":"0lt","Selected":false},
{"ParentID":null,"Value":41,"Text":"GaW","Selected":false},
{"ParentID":3,"Value":42,"Text":"w03","Selected":false},
{"ParentID":3,"Value":43,"Text":"+AK","Selected":false},
{"ParentID":3,"Value":44,"Text":"rHp","Selected":false},
{"ParentID":3,"Value":45,"Text":"mZL","Selected":false},
{"ParentID":3,"Value":46,"Text":"+Xt","Selected":false},
{"ParentID":40,"Value":47,"Text":"+Xt","Selected":false},
{"ParentID":40,"Value":48,"Text":"jv/","Selected":false},
{"ParentID":40,"Value":49,"Text":"J7m","Selected":false},
{"ParentID":40,"Value":50,"Text":"jv/","Selected":false},
{"ParentID":40,"Value":51,"Text":"x7z","Selected":false},
{"ParentID":40,"Value":52,"Text":"jv/","Selected":false},
{"ParentID":40,"Value":53,"Text":"1tz","Selected":false},
{"ParentID":40,"Value":54,"Text":"jv/","Selected":false},
{"ParentID":40,"Value":55,"Text":"Jks","Selected":false},
{"ParentID":40,"Value":56,"Text":"cMU","Selected":false},
{"ParentID":40,"Value":57,"Text":"Jks","Selected":false},
{"ParentID":40,"Value":58,"Text":"m6m","Selected":false},
{"ParentID":40,"Value":59,"Text":"FBR","Selected":false},
{"ParentID":40,"Value":60,"Text":"FBR","Selected":false},
{"ParentID":40,"Value":61,"Text":"6+K","Selected":false},
{"ParentID":40,"Value":62,"Text":"6+K","Selected":false},
{"ParentID":null,"Value":63,"Text":"vOw","Selected":false},
{"ParentID":63,"Value":64,"Text":"vOw","Selected":false},
{"ParentID":40,"Value":65,"Text":"UJq","Selected":false},
{"ParentID":40,"Value":66,"Text":"UJq","Selected":false},
{"ParentID":63,"Value":67,"Text":"vOw","Selected":false},
{"ParentID":null,"Value":68,"Text":"DQZ","Selected":false},
{"ParentID":68,"Value":69,"Text":"DQZ","Selected":false},
{"ParentID":68,"Value":70,"Text":"DQZ","Selected":false},
{"ParentID":68,"Value":71,"Text":"DQZ","Selected":false}

]

我正在寻找一个使用$.each的嵌套循环,其中外循环是

ParendID=null

和内部循环是

value=ParendID 

类似的东西。

$.each(ds.Where(o=>o.ParendID==null),functhion(index,item){
     //....sum code here....
     $.each(item,function(index1,inneritem){
         //....sum code here....
     });
});

有什么简单的方法。

2 个答案:

答案 0 :(得分:1)

Array.prototype.filterArray.prototype.forEach将完成这项工作

var ds = [
  {"ParentID":39,"Value":1,"Text":"LM7","Selected":false},
  {"ParentID":41,"Value":2,"Text":"ud5","Selected":false},
  {"ParentID":null,"Value":14,"Text":"w03","Selected":false},
  {"ParentID":null,"Value":15,"Text":"w03","Selected":false},
  {"ParentID":null,"Value":16,"Text":"w03","Selected":false},
  {"ParentID":null,"Value":17,"Text":"SFt","Selected":false},
  {"ParentID":null,"Value":18,"Text":"XlG","Selected":false},
  {"ParentID":null,"Value":39,"Text":"D6a","Selected":false},
  {"ParentID":null,"Value":40,"Text":"0lt","Selected":false},
  {"ParentID":null,"Value":41,"Text":"GaW","Selected":false},
  {"ParentID":40,"Value":62,"Text":"6+K","Selected":false},
  {"ParentID":null,"Value":63,"Text":"vOw","Selected":false},
  {"ParentID":63,"Value":64,"Text":"vOw","Selected":false}
];

ds.filter((o) => o.ParentID === null)
  .forEach((o) => console.log(o));

答案 1 :(得分:0)

您可以使用$.grep()过滤ParentID的值为null的对象

var items = $.grep(ds, (o) => o.ParentID == null);
$.each(items, (index1, inneritem) => /* do stuff */);