gpt4 book ai didi

javascript - 获取父数组及其所有子数组

转载 作者:数据小太阳 更新时间:2023-10-29 05:22:04 25 4
gpt4 key购买 nike

假设我有这种数据...

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>",
}]

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

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

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

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

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

我试过了(不知道)...

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

不工作...

任何有关 javascript/lodash/underscore 的帮助将不胜感激,,,

谢谢你...

最佳答案

这是另一种解释,使用 native Array.prototype.reduce 方法仅将子元素添加到返回的数组。

edit, didn't read question properly, this will now return the current id and all children.

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 getChildIds( arr, id ){
var parentFound = false;
return arr.reduce(function( ret, item ){
if( parentFound === false && item._id == id ){
parentFound = true;
}

if( parentFound ) {
ret = ret.concat( item._id );
}

return ret;
}, []);
}

console.log( getChildIds(data, '1') );
console.log( getChildIds(data, '2') );
console.log( getChildIds(data, '3') );
console.log( getChildIds(data, '4') );
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

any order, not sure why it's necessary thought.

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

function getChildIdsInAnyOrder(arr, id) {
return arr.reduce(function(ret, item) {
if ( parseInt(item._id) >= parseInt(id) ) {
ret = ret.concat(item._id);
}
return ret;
}, []);
}

console.log(getChildIdsInAnyOrder(data, '1'));
console.log(getChildIdsInAnyOrder(data, '2'));
console.log(getChildIdsInAnyOrder(data, '3'));
console.log(getChildIdsInAnyOrder(data, '4'));
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

关于javascript - 获取父数组及其所有子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34719661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com