我从表值生成JSON数组,在表I中存储Id,Parent_id和Data以及其他一些值。如果Parent_id没有任何父级,则为NULL
,如果是,则它将引用父级ID。
[{"state":"open","data":"Signature","attr":
{"id":0,"item_id":2,"parent_id":0},"children":[{"data":"or replace","attr":
{"id":1,"item_id":3,"parent_id":2}}]},{"state":"open","data":"Parameter","attr":
{"id":0,"item_id":4,"parent_id":0},"children":[{"data":"in parameter","attr":
{"id":3,"item_id":5,"parent_id":4}},{"data":"in out parameter","attr":
{"id":4,"item_id":6,"parent_id":4}}]},{"state":"open","data":"Mandatory Block","attr":
{"id":0,"item_id":7,"parent_id":0},"children":[{"data":"validate Exit condition for loop","attr":{"id":8,"item_id":8,"parent_id":7}},{"data":"validate exception","attr":
{"id":7,"item_id":9,"parent_id":7}}]},{"state":"open","data":"parent","attr":
{"id":0,"item_id":10,"parent_id":7},"children":[{"data":"parent_CHILD","attr":
{"id":2,"item_id":11,"parent_id":10}},{"data":"parent_CHILD1","attr":
{"id":5,"item_id":12,"parent_id":10}}]}]
从JSON上面,您可以看到parent_id=0
表示它没有父级。现在,我想根据parent_id和Item_id重建它,这意味着如果parent_id=0
然后根据item_id对其进行排序
帮我解决一下。
更新
我试过跟随,但无法得到欲望的结果。
<script src='js/linq.js'></script>
<script src='js/jquery.js'></script>
<script>
var dataArray = $.parseJSON([{"state":"open","data":"Signature","attr":
{"id":0,"item_id":2,"parent_id":0},"children":[{"data":"or replace","attr":
{"id":1,"item_id":3,"parent_id":2}}]},{"state":"open","data":"Parameter","attr":
{"id":0,"item_id":4,"parent_id":0},"children":[{"data":"in parameter","attr":
{"id":3,"item_id":5,"parent_id":4}},{"data":"in out parameter","attr":
{"id":4,"item_id":6,"parent_id":4}}]},{"state":"open","data":"Mandatory Block","attr":
{"id":0,"item_id":7,"parent_id":0},"children":[{"data":"validate Exit condition for loop","attr":{"id":8,"item_id":8,"parent_id":7}},{"data":"validate exception","attr":
{"id":7,"item_id":9,"parent_id":7}}]},{"state":"open","data":"parent","attr":
{"id":0,"item_id":10,"parent_id":7},"children":[{"data":"parent_CHILD","attr":
{"id":2,"item_id":11,"parent_id":10}},{"data":"parent_CHILD1","attr":
{"id":5,"item_id":12,"parent_id":10}}]}]);
var sorted = Enumerable.From(dataArray)
.OrderBy(function(x) {
return x.parent_id;
})
.ThenBy(function(x) {
return x.item_id;
})
.ToArray();
console.log(sorted);
console.log(dataArray);
</script>
答案 0 :(得分:0)
请看linq.js。
var dataArray = $.parseJSON(dataInYourQuestion);
var sorted = Enumerable.From(dataArray)
.OrderBy(function(x) {
return x.parent_id;
})
.ThenBy(function(x) {
return x.item_id;
})
.ToArray();