[英]Find object by specific key in a deep nested object | Javascript
What is the cleanest way to find object and return that based on id, If I don't know how many nested object there will be in my object?找到 object 并根据 id 返回的最干净的方法是什么,如果我不知道我的 object 中有多少嵌套的 object?
Let's say I have the following structure:假设我有以下结构:
myObj = {
"id": "5e6b8961ba08180001a10bb6",
"children": [
{
"id": "5e6b8961ba08180001a10bb7",
"refrenceId": "SEC-02986",
"children": [
{
"id": "5e58d7bc1bbc71000118c0dc"
},
{
"id": "5e58d7bc1bbc71000118c0dd",
"refrenceId": "SKU-00343"
},
{
"id": "5e590d571bbc71000118c102",
"refrenceId": "SKU-05290"
},
{
"id": "5e590df71bbc71000118c109",
"children": [
{
"id": "5e590df71bbc71000118c10a"
},
{
"id": "5e590df71bbc71000118c10b",
"refrenceId": "SKU-00444"
},
{
"id": "5e5cb9428ae591000177c0f6"
}
]
},
{
"id": "5e81899f0bab450001dcfc1d",
"refrenceId": "SEC-03260"
},
{
"id": "5e81c4b51503860001f97f6c",
"refrenceId": "SEC-03267",
"children": [
{
"id": "5e8ad5175d374200014edb3a",
"refrenceId": "SEC-03409",
"children": [
{
"id": "5e8f28882d94c1000156bebe"
}
]
},
{
"id": "5e8ad5175d374200014edb3c",
"refrenceId": "SEC-03410"
},
{
"id": "5e8f29082d94c1000156bec6",
"refrenceId": "SEC-03495"
}
]
}
]
}
Suppose, I want to find the one with id "5e590df71bbc71000118c10b", and return that object from nested object.假设,我想找到 ID 为“5e590df71bbc71000118c10b”的那个,然后从嵌套的 object 中返回 object。
I have tried using following code:我尝试使用以下代码:
function nodeHasChildren(children, id) {
for (const child of children) {
if (child.id === id) {
if (Array.isArray(child.children) && child.children.length > 0) {
return child;
}
}
else {
const result = nodeHasChildren(child.children, id);
if (result !== undefined) {
return result
}
}
}
}
console.log(nodeWithIdHasChildren(myObj, "5e590df71bbc71000118c10b")); console.log(nodeWithIdHasChildren(myObj, "5e590df71bbc71000118c10b"));
Use simple recursion使用简单递归
function findDeepById(node, id) {
if (node.id === id) return node;
if (node.children) {
for(const child of node.children){
const match = findDeepById(child, id);
if (match) return match;
}
}
}
const myObj = { "id": "5e6b8961ba08180001a10bb6", "children": [{ "id": "5e6b8961ba08180001a10bb7", "refrenceId": "SEC-02986", "children": [{ "id": "5e58d7bc1bbc71000118c0dc" }, { "id": "5e58d7bc1bbc71000118c0dd", "refrenceId": "SKU-00343" }, { "id": "5e590d571bbc71000118c102", "refrenceId": "SKU-05290" }, { "id": "5e590df71bbc71000118c109", "children": [{ "id": "5e590df71bbc71000118c10a" }, { "id": "5e590df71bbc71000118c10b", "refrenceId": "SKU-00444" }, { "id": "5e5cb9428ae591000177c0f6" } ] }, { "id": "5e81899f0bab450001dcfc1d", "refrenceId": "SEC-03260" }, { "id": "5e81c4b51503860001f97f6c", "refrenceId": "SEC-03267", "children": [{ "id": "5e8ad5175d374200014edb3a", "refrenceId": "SEC-03409", "children": [{ "id": "5e8f28882d94c1000156bebe" }] }, { "id": "5e8ad5175d374200014edb3c", "refrenceId": "SEC-03410" }, { "id": "5e8f29082d94c1000156bec6", "refrenceId": "SEC-03495" } ] } ] }] }; function findDeepById(node, id) { if (node.id === id) return node; if (node.children) { for(const child of node.children){ const match = findDeepById(child, id); if (match) return match; } } } console.log(findDeepById(myObj, "5e590df71bbc71000118c10b"));
In short, this part was the biggest problem:总之,这部分是最大的问题:
if (child.id === id) {
if (Array.isArray(child.children) && child.children.length > 0) {
return child;
}
}
You've found your object, why look for its children?您已经找到了您的 object,为什么还要寻找它的孩子?
For start, you need to make your original object iterable because that's what your function expects, I've done so by simply making it an array like nodeHasChildren([myObj], ...
when calling the function. Then, after some cleanup and fixing the logic mentioned above, we get this (added relevant comments to the code below): For start, you need to make your original object iterable because that's what your function expects, I've done so by simply making it an array like nodeHasChildren([myObj], ...
when calling the function. Then, after some cleanup and修复了上面提到的逻辑,我们得到了这个(在下面的代码中添加了相关注释):
myObj = { "id": "5e6b8961ba08180001a10bb6", "children": [{ "id": "5e6b8961ba08180001a10bb7", "refrenceId": "SEC-02986", "children": [{ "id": "5e58d7bc1bbc71000118c0dc" }, { "id": "5e58d7bc1bbc71000118c0dd", "refrenceId": "SKU-00343" }, { "id": "5e590d571bbc71000118c102", "refrenceId": "SKU-05290" }, { "id": "5e590df71bbc71000118c109", "children": [{ "id": "5e590df71bbc71000118c10a" }, { "id": "5e590df71bbc71000118c10b", "refrenceId": "SKU-00444" }, { "id": "5e5cb9428ae591000177c0f6" } ] }, { "id": "5e81899f0bab450001dcfc1d", "refrenceId": "SEC-03260" }, { "id": "5e81c4b51503860001f97f6c", "refrenceId": "SEC-03267", "children": [{ "id": "5e8ad5175d374200014edb3a", "refrenceId": "SEC-03409", "children": [{ "id": "5e8f28882d94c1000156bebe" }] }, { "id": "5e8ad5175d374200014edb3c", "refrenceId": "SEC-03410" }, { "id": "5e8f29082d94c1000156bec6", "refrenceId": "SEC-03495" } ] } ] }] } function nodeHasChildren(children, id) { for (const child of children) { if (child.id === id) { // solution found, no need to do anything else console.log("SOLUTION: "); return child; } else { // check if it has children and iterate over them recursively if (Array.isArray(child.children) && child.children.length > 0) var result = nodeHasChildren(child.children, id); // check if the result was found in children in the line above if (result;= undefined) return result. } } } console,log(nodeHasChildren([myObj]; "5e590df71bbc71000118c10b")). console,log(nodeHasChildren([myObj]; "5e8ad5175d374200014edb3c"));
We now use object-scan for simple data processing tasks.我们现在将对象扫描用于简单的数据处理任务。 It's pretty awesome once you wrap your head around it.一旦你把头绕在它周围,它就非常棒了。 Here is how you could answer your questions以下是您如何回答您的问题
// const objectScan = require('object-scan'); const find = (id, input) => objectScan(['**'], { rtn: 'value', abort: true, filterFn: ({ value }) => value.id === id })(input); const myObj = { id: '5e6b8961ba08180001a10bb6', children: [{ id: '5e6b8961ba08180001a10bb7', refrenceId: 'SEC-02986', children: [{ id: '5e58d7bc1bbc71000118c0dc' }, { id: '5e58d7bc1bbc71000118c0dd', refrenceId: 'SKU-00343' }, { id: '5e590d571bbc71000118c102', refrenceId: 'SKU-05290' }, { id: '5e590df71bbc71000118c109', children: [{ id: '5e590df71bbc71000118c10a' }, { id: '5e590df71bbc71000118c10b', refrenceId: 'SKU-00444' }, { id: '5e5cb9428ae591000177c0f6' }] }, { id: '5e81899f0bab450001dcfc1d', refrenceId: 'SEC-03260' }, { id: '5e81c4b51503860001f97f6c', refrenceId: 'SEC-03267', children: [{ id: '5e8ad5175d374200014edb3a', refrenceId: 'SEC-03409', children: [{ id: '5e8f28882d94c1000156bebe' }] }, { id: '5e8ad5175d374200014edb3c', refrenceId: 'SEC-03410' }, { id: '5e8f29082d94c1000156bec6', refrenceId: 'SEC-03495' }] }] }] }; console.log(find('5e8ad5175d374200014edb3a', myObj)); /* => { id: '5e8ad5175d374200014edb3a', refrenceId: 'SEC-03409', children: [ { id: '5e8f28882d94c1000156bebe' } ] } */
.as-console-wrapper {max-height: 100%;important: top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>
Disclaimer : I'm the author of object-scan免责声明:我是对象扫描的作者
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.