Javascript - 计算 JSON 的所有嵌套 objects
Javascript - Couting all nested objects of JSON
假设我有以下 JSON:
{
"id": "foo",
"list": [
{
"id": "A",
"list": [
{
"id": "B",
"list": [
{
"id": "C",
"list": [
{
"id": "D",
"list": []
},
{
"id": "E",
"list": []
}
]
},
{
"id": "F",
"list": []
},
{
"id": "G",
"list": [
{
"id": "H",
"list": []
},
{
"id": "I",
"list": []
},
{
"id": "J",
"list": []
}
]
}
]
},
{
"id": "K",
"list": []
}
]
},
{
"id": "L",
"list": [
{
"id": "M",
"list": []
}
]
},
{
"id": "N",
"list": []
},
{
"id": "O",
"list": [
{
"id": "P",
"list": [
{
"id": "Q",
"list": []
},
{
"id": "R",
"list": []
},
{
"id": "S",
"list": []
},
{
"id": "T",
"list": [
{
"id": "U",
"list": []
}
]
},
{
"id": "V",
"list": [
{
"id": "W",
"list": [
{
"id": "X",
"list": []
},
{
"id": "Y",
"list": []
},
{
"id": "Z",
"list": []
}
]
}
]
}
]
}
]
}
]
}
我的问题是:我如何计算每个孩子并将此数字附加到每个 object 的 属性 中?
示例:
- "C" object 有 2 objects inside and on subobjects, "D" and "E".
- "W" object 内部和子object 上有 3 个 object,"X","Y" 和 "Z".
- "V" ovject 有(这里是技巧)4 objects inside and on sub objects,"W" object 本身及其所有子项(前面提到的 3 个)。
关于这个,"C"object应该有一个属性,我们把它命名为"allBelow",包含数字2。"W"object 包含 3,而 "V" object 包含 4。依此类推,对于每个 object.
我想知道一些递归函数可以完成这项工作,但我没有实现它。
你能帮帮我吗?
最佳,
深度优先搜索应该可行,我在想这样的事情(未测试代码):
function DFS(tree){
var currentCount = tree.list.length;
for(var i=0;i<count;i++){
currentCount += DFS(tree.list[i]);
}
tree["count"] = currentCount;
return currentCount;
}
递归函数是个好主意。试试这个:
var data = {"id":"foo","list":[{"id":"A","list":[{"id":"B","list":[{"id":"C","list":[{"id":"D","list":[]},{"id":"E","list":[]}]},{"id":"F","list":[]},{"id":"G","list":[{"id":"H","list":[]},{"id":"I","list":[]},{"id":"J","list":[]}]}]},{"id":"K","list":[]}]},{"id":"L","list":[{"id":"M","list":[]}]},{"id":"N","list":[]},{"id":"O","list":[{"id":"P","list":[{"id":"Q","list":[]},{"id":"R","list":[]},{"id":"S","list":[]},{"id":"T","list":[{"id":"U","list":[]}]},{"id":"V","list":[{"id":"W","list":[{"id":"X","list":[]},{"id":"Y","list":[]},{"id":"Z","list":[]}]}]}]}]}]};
function addCount(node) {
node.count = 0;
for (var i = 0; i < node.list.length; i++) {
var child = node.list[i];
addCount(child);
node.count += child.count + 1;
}
}
addCount(data);
console.log(data)
它首先在每个 child 节点上调用自己。然后它将每个 child 添加到计数中作为 1 + grandchildren(或 grand-grand- 或更多)的数量。
var myObj = {"id":"foo","list":[{"id":"A","list":[{"id":"B","list":[{"id":"C","list":[{"id":"D","list":[]},{"id":"E","list":[]}]},{"id":"F","list":[]},{"id":"G","list":[{"id":"H","list":[]},{"id":"I","list":[]},{"id":"J","list":[]}]}]},{"id":"K","list":[]}]},{"id":"L","list":[{"id":"M","list":[]}]},{"id":"N","list":[]},{"id":"O","list":[{"id":"P","list":[{"id":"Q","list":[]},{"id":"R","list":[]},{"id":"S","list":[]},{"id":"T","list":[{"id":"U","list":[]}]},{"id":"V","list":[{"id":"W","list":[{"id":"X","list":[]},{"id":"Y","list":[]},{"id":"Z","list":[]}]}]}]}]}]};
function count(obj) {
var c = obj.list.length;
c += obj.list.reduce((a, e) => a + count(e), 0);
obj.count = c; // assign the count after counting the subobjects.
return c; // return the count to be used by parent objects
}
count(myObj);
console.log(myObj);
你可以做一个简单的 DFS:
function appendNumChildren(currentNode) {
const totalChildren = currentNode.list.reduce((acc, node) => {
return acc + appendNumChildren(node);
}, 0)
currentNode.allBelow = totalChildren;
return totalChildren + 1;
}
appendNumChildren(json);
假设我有以下 JSON:
{
"id": "foo",
"list": [
{
"id": "A",
"list": [
{
"id": "B",
"list": [
{
"id": "C",
"list": [
{
"id": "D",
"list": []
},
{
"id": "E",
"list": []
}
]
},
{
"id": "F",
"list": []
},
{
"id": "G",
"list": [
{
"id": "H",
"list": []
},
{
"id": "I",
"list": []
},
{
"id": "J",
"list": []
}
]
}
]
},
{
"id": "K",
"list": []
}
]
},
{
"id": "L",
"list": [
{
"id": "M",
"list": []
}
]
},
{
"id": "N",
"list": []
},
{
"id": "O",
"list": [
{
"id": "P",
"list": [
{
"id": "Q",
"list": []
},
{
"id": "R",
"list": []
},
{
"id": "S",
"list": []
},
{
"id": "T",
"list": [
{
"id": "U",
"list": []
}
]
},
{
"id": "V",
"list": [
{
"id": "W",
"list": [
{
"id": "X",
"list": []
},
{
"id": "Y",
"list": []
},
{
"id": "Z",
"list": []
}
]
}
]
}
]
}
]
}
]
}
我的问题是:我如何计算每个孩子并将此数字附加到每个 object 的 属性 中?
示例:
- "C" object 有 2 objects inside and on subobjects, "D" and "E".
- "W" object 内部和子object 上有 3 个 object,"X","Y" 和 "Z".
- "V" ovject 有(这里是技巧)4 objects inside and on sub objects,"W" object 本身及其所有子项(前面提到的 3 个)。
关于这个,"C"object应该有一个属性,我们把它命名为"allBelow",包含数字2。"W"object 包含 3,而 "V" object 包含 4。依此类推,对于每个 object.
我想知道一些递归函数可以完成这项工作,但我没有实现它。
你能帮帮我吗?
最佳,
深度优先搜索应该可行,我在想这样的事情(未测试代码):
function DFS(tree){
var currentCount = tree.list.length;
for(var i=0;i<count;i++){
currentCount += DFS(tree.list[i]);
}
tree["count"] = currentCount;
return currentCount;
}
递归函数是个好主意。试试这个:
var data = {"id":"foo","list":[{"id":"A","list":[{"id":"B","list":[{"id":"C","list":[{"id":"D","list":[]},{"id":"E","list":[]}]},{"id":"F","list":[]},{"id":"G","list":[{"id":"H","list":[]},{"id":"I","list":[]},{"id":"J","list":[]}]}]},{"id":"K","list":[]}]},{"id":"L","list":[{"id":"M","list":[]}]},{"id":"N","list":[]},{"id":"O","list":[{"id":"P","list":[{"id":"Q","list":[]},{"id":"R","list":[]},{"id":"S","list":[]},{"id":"T","list":[{"id":"U","list":[]}]},{"id":"V","list":[{"id":"W","list":[{"id":"X","list":[]},{"id":"Y","list":[]},{"id":"Z","list":[]}]}]}]}]}]};
function addCount(node) {
node.count = 0;
for (var i = 0; i < node.list.length; i++) {
var child = node.list[i];
addCount(child);
node.count += child.count + 1;
}
}
addCount(data);
console.log(data)
它首先在每个 child 节点上调用自己。然后它将每个 child 添加到计数中作为 1 + grandchildren(或 grand-grand- 或更多)的数量。
var myObj = {"id":"foo","list":[{"id":"A","list":[{"id":"B","list":[{"id":"C","list":[{"id":"D","list":[]},{"id":"E","list":[]}]},{"id":"F","list":[]},{"id":"G","list":[{"id":"H","list":[]},{"id":"I","list":[]},{"id":"J","list":[]}]}]},{"id":"K","list":[]}]},{"id":"L","list":[{"id":"M","list":[]}]},{"id":"N","list":[]},{"id":"O","list":[{"id":"P","list":[{"id":"Q","list":[]},{"id":"R","list":[]},{"id":"S","list":[]},{"id":"T","list":[{"id":"U","list":[]}]},{"id":"V","list":[{"id":"W","list":[{"id":"X","list":[]},{"id":"Y","list":[]},{"id":"Z","list":[]}]}]}]}]}]};
function count(obj) {
var c = obj.list.length;
c += obj.list.reduce((a, e) => a + count(e), 0);
obj.count = c; // assign the count after counting the subobjects.
return c; // return the count to be used by parent objects
}
count(myObj);
console.log(myObj);
你可以做一个简单的 DFS:
function appendNumChildren(currentNode) {
const totalChildren = currentNode.list.reduce((acc, node) => {
return acc + appendNumChildren(node);
}, 0)
currentNode.allBelow = totalChildren;
return totalChildren + 1;
}
appendNumChildren(json);