简体   繁体   English

按值在深层嵌套 object 中查找 object

[英]Find object in deep nested object by value

I would like to find an object id in deep nested object by value.我想按值在深层嵌套的 object 中找到一个 object id I try to do it with recursion, but can't figure out why got 'undefined'.我尝试用递归来做到这一点,但无法弄清楚为什么会出现“未定义”。

In this code I have in console "final result: 234324234" which is correct, but for some reason this result is not returning from this function.在这段代码中,我在控制台"final result: 234324234"是正确的,但由于某种原因,这个结果没有从这个 function 返回。

Please, take a look.请看一下。

demo in jsbin jsbin 中的演示

 let obj = { "uuid": "344444", "entityName": "priceFormationPhase", "id": 2, "value": "foo", "children": { "4": { "uuid": "44444", "entityName": "organization", "id": 4, "value": "ffffff", "children": { "344534": { "uuid": "33333", "entityName": "contract", "id": 928688, "value": "dh", "children": { "345345": { "uuid": "222222222", "entityName": "contractPhase", "id": 234324234, "value": "111", "children": {} } } } } } } }; function findContractStage(obj) { if ((typeof obj.children === 'object') && (Object.keys(obj.children).length > 0)) { findContractStage(obj.children); } else if (typeof obj[Object.keys(obj)[0]] === 'object') { findContractStage(obj[Object.keys(obj)[0]]); } else if (obj.entityName) { console.log(`final result: ${obj.id}`); return obj.id; } } let contractStageId = findContractStage(obj); console.log(`contractStageId: ${contractStageId}`);

You're not returning the value you get when you recurse.您不会返回递归时获得的值。 Try this:尝试这个:

 let obj = { "uuid": "344444", "entityName": "priceFormationPhase", "id": 2, "value": "foo", "children": { "4": { "uuid": "44444", "entityName": "organization", "id": 4, "value": "ffffff", "children": { "344534": { "uuid": "33333", "entityName": "contract", "id": 928688, "value": "dh", "children": { "345345": { "uuid": "222222222", "entityName": "contractPhase", "id": 234324234, "value": "111", "children": {} } } } } } } }; function findContractStage(obj) { if ((typeof obj.children === 'object') && (Object.keys(obj.children).length > 0)) { return findContractStage(obj.children); } else if (typeof obj[Object.keys(obj)[0]] === 'object') { return findContractStage(obj[Object.keys(obj)[0]]); } else if (obj.entityName) { console.log(`final result: ${obj.id}`); return obj.id; } } let contractStageId = findContractStage(obj); console.log(`contractStageId: ${contractStageId}`);

You forget to return the function您忘记return function

 let obj = { "uuid": "344444", "entityName": "priceFormationPhase", "id": 2, "value": "foo", "children": { "4": { "uuid": "44444", "entityName": "organization", "id": 4, "value": "ffffff", "children": { "344534": { "uuid": "33333", "entityName": "contract", "id": 928688, "value": "dh", "children": { "345345": { "uuid": "222222222", "entityName": "contractPhase", "id": 234324234, "value": "111", "children": {} } } } } } } }; function findContractStage(obj) { if ((typeof obj.children === 'object') && (Object.keys(obj.children).length > 0)) { return findContractStage(obj.children); } else if (typeof obj[Object.keys(obj)[0]] === 'object') { return findContractStage(obj[Object.keys(obj)[0]]); } else if (obj.entityName) { console.log(`final result: ${obj.id}`); return obj.id; } } let contractStageId = findContractStage(obj); console.log(`contractStageId: ${contractStageId}`);

You need to add "return" before recursive calls您需要在递归调用之前添加“return”

return findContractStage(obj.children);

and

return findContractStage(obj[Object.keys(obj)[0]]);

So your recursive function goes deeper as it can and return your id value.因此,您的递归 function 会尽可能深入并返回您的 id 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM