Javascript 压平深层嵌套 children
Javascript flatten deep nested children
这个我真的想不通。我正在尝试展平特定节点 child 的 category_id
。
var categories = [{
"category_id": "66",
"parent_id": "59"
}, {
"category_id": "68",
"parent_id": "67",
}, {
"category_id": "69",
"parent_id": "59"
}, {
"category_id": "59",
"parent_id": "0",
}, {
"category_id": "67",
"parent_id": "66"
}, {
"category_id": "69",
"parent_id": "59"
}];
或视觉上:
我最接近的是递归循环找到的第一个项目:
function children(category) {
var children = [];
var getChild = function(curr_id) {
// how can I handle all of the cats, and not only the first one?
return _.first(_.filter(categories, {
'parent_id': String(curr_id)
}));
};
var curr = category.category_id;
while (getChild(curr)) {
var child = getChild(curr).category_id;
children.push(child);
curr = child;
}
return children;
}
children(59)
的当前输出是['66', '67', '68']
。
预期输出为 ['66', '67', '68', '69']
我没有测试,但应该可以:
function getChildren(id, categories) {
var children = [];
_.filter(categories, function(c) {
return c["parent_id"] === id;
}).forEach(function(c) {
children.push(c);
children = children.concat(getChildren(c.category_id, categories));
})
return children;
}
我正在使用 lodash。
编辑:我测试了它,现在它应该可以工作了。见 plunker:https://plnkr.co/edit/pmENXRl0yoNnTczfbEnT?p=preview
这是一个小的优化,您可以通过丢弃筛选的类别来进行。
function getChildren(id, categories) {
var children = [];
var notMatching = [];
_.filter(categories, function(c) {
if(c["parent_id"] === id)
return true;
else
notMatching.push(c);
}).forEach(function(c) {
children.push(c);
children = children.concat(getChildren(c.category_id, notMatching));
})
return children;
}
这个我真的想不通。我正在尝试展平特定节点 child 的 category_id
。
var categories = [{
"category_id": "66",
"parent_id": "59"
}, {
"category_id": "68",
"parent_id": "67",
}, {
"category_id": "69",
"parent_id": "59"
}, {
"category_id": "59",
"parent_id": "0",
}, {
"category_id": "67",
"parent_id": "66"
}, {
"category_id": "69",
"parent_id": "59"
}];
或视觉上:
我最接近的是递归循环找到的第一个项目:
function children(category) {
var children = [];
var getChild = function(curr_id) {
// how can I handle all of the cats, and not only the first one?
return _.first(_.filter(categories, {
'parent_id': String(curr_id)
}));
};
var curr = category.category_id;
while (getChild(curr)) {
var child = getChild(curr).category_id;
children.push(child);
curr = child;
}
return children;
}
children(59)
的当前输出是['66', '67', '68']
。
预期输出为 ['66', '67', '68', '69']
我没有测试,但应该可以:
function getChildren(id, categories) {
var children = [];
_.filter(categories, function(c) {
return c["parent_id"] === id;
}).forEach(function(c) {
children.push(c);
children = children.concat(getChildren(c.category_id, categories));
})
return children;
}
我正在使用 lodash。
编辑:我测试了它,现在它应该可以工作了。见 plunker:https://plnkr.co/edit/pmENXRl0yoNnTczfbEnT?p=preview
这是一个小的优化,您可以通过丢弃筛选的类别来进行。
function getChildren(id, categories) {
var children = [];
var notMatching = [];
_.filter(categories, function(c) {
if(c["parent_id"] === id)
return true;
else
notMatching.push(c);
}).forEach(function(c) {
children.push(c);
children = children.concat(getChildren(c.category_id, notMatching));
})
return children;
}