获取父级及其所有子级的数组 [英] get array of parent and all of its child

查看:51
本文介绍了获取父级及其所有子级的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这种数据...

  data = [{"_id":"1","parentId":"thisPostId","topLevelId":"1","text":&p; comment</p>",},{"_id":"2","parentId":"1","topLevelId":"1","text":< p>回复评论</p>",},{"_id":"3","parentId":"2","topLevelId":"1","text":< p>回复评论</p>",},{"_id":"4","parentId":"3","topLevelId":"1","text":< p>回复回复,回复评论</p>",}] 

我需要删除评论及其所有子级...

如果要删除的注释是 _id:1 ,那么我需要一个数组 ["1","2","3","4"] ,,然后我可以运行 Coll.remove({_ id:{$ in:["1","2","3","4"]}},回调);

如果要删除的注释是 _id:2 ,那么我需要一个数组 ["2","3","4"]

如果要删除的注释是 _id:3 ,那么我需要一个数组 ["3","4"]

如果要删除的评论是 _id:4 ,那么我需要一个 ["4"]

数组

我尝试了这个(不知道)...

  _.forEach(数据,函数(值,键){_.pluck(_.where(key,{"parentId":"2"}),'_id');}); 

不起作用...

任何有关javascript/lodash/underscore的帮助,

谢谢...

解决方案

这是带有临时对象和对ID的递归调用的提议.

临时对象 o 包含所有ID及其子对象

  {"1":["2"],"2":["3"],"3":["4"],"thisPostId":["1"]} 

在构建此对象之后,将使用查找的ID并检查该对象是否包含属性.尽管所有peoperty都是数组,但是可以遍历 go()并获取所有ID进行收集.如果还有另一个孩子,则递归迭代正在进行.

  var data = [{"_id":"1","parentId":"thisPostId","topLevelId":"1","text":< p> comment</p>",},{"_id":"2","parentId":"1","topLevelId":"1","text":<p>回复评论</p>,},{" _id:" 3," parentId:" 2," topLevelId:" 1," text:"< p>回复to comment</p>,},{" _id:" 4," parentId:" 3," topLevelId:" 1," text:"< p>回复comment</p>,}];函数getConnected(s){函数go(a){r.push(a);o [a]&&o [a] .forEach(go);}var o = data.reduce(function(r,a){r [a.parentId] = r [a.parentId] ||[];r [a.parentId] .push(a._id);返回r;},{}),r = [s];o [s]&&o [s] .forEach(go);返回r;}对于(var i = 1; i< = 4; i ++){document.write(''+ i +'":'+ JSON.stringify(getConnected(i.toString()))+'< br>');}  

suppose I have this kind of data...

data = [{
    "_id" : "1",
    "parentId" : "thisPostId",
    "topLevelId" : "1",
    "text" : "<p>comment</p>",
},
{
    "_id" : "2",
    "parentId" : "1",
    "topLevelId" : "1",
    "text" : "<p>reply to comment</p>",
},
{
    "_id" : "3",
    "parentId" : "2",
    "topLevelId" : "1",
    "text" : "<p>reply to reply to comment</p>",
},
{
    "_id" : "4",
    "parentId" : "3",
    "topLevelId" : "1",
    "text" : "<p>reply to reply to reply to comment</p>",
}]

I need to remove a comment and all of its child...

if comment to remove is _id:1,, then I need an array of ["1","2","3","4"],,, then i can run Coll.remove({_id:{$in:["1","2","3","4"]}}, callback);

if comment to remove is _id:2,, then I need an array of ["2","3","4"]

if comment to remove is _id:3,, then I need an array of ["3","4"]

if comment to remove is _id:4,, then I need an array of ["4"]

I tried this (with no idea)...

_.forEach(data, function(value, key){
    _.pluck(_.where(key, { "parentId" : "2" }), '_id');
});

and not working...

any help with javascript/lodash/underscore will be appreciated,,,

thank You...

解决方案

This is a proposal with a temporary object and a recursive call for the ids.

The temporary object o contains all ids and their childrens

{
    "1": ["2"],
    "2": ["3"],
    "3": ["4"],
    "thisPostId": ["1"]
}

After this object is build, the id for the look up is taken and checked if the object contains the property. While all peopertys are arrays, it is possible to iterate over go() and get all id for collecting. If there is another child, the recursive iteration is going on.

var data = [{ "_id": "1", "parentId": "thisPostId", "topLevelId": "1", "text": "<p>comment</p>", }, { "_id": "2", "parentId": "1", "topLevelId": "1", "text": "<p>reply to comment</p>", }, { "_id": "3", "parentId": "2", "topLevelId": "1", "text": "<p>reply to reply to comment</p>", }, { "_id": "4", "parentId": "3", "topLevelId": "1", "text": "<p>reply to reply to reply to comment</p>", }];

function getConnected(s) {
    function go(a) { r.push(a); o[a] && o[a].forEach(go); }

    var o = data.reduce(function (r, a) {
            r[a.parentId] = r[a.parentId] || [];
            r[a.parentId].push(a._id);
            return r;                
        }, {}),
        r = [s];

    o[s] && o[s].forEach(go);
    return r;
}

for (var i = 1; i <= 4; i++) {
    document.write('"' + i + '": ' + JSON.stringify(getConnected(i.toString())) + '<br>');
}

这篇关于获取父级及其所有子级的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆