[英]Find object in deep nested object by value
我想按值在深層嵌套的 object 中找到一個 object id
。 我嘗試用遞歸來做到這一點,但無法弄清楚為什么會出現“未定義”。
在這段代碼中,我在控制台"final result: 234324234"
是正確的,但由於某種原因,這個結果沒有從這個 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)) { 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}`);
您不會返回遞歸時獲得的值。 嘗試這個:
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}`);
您忘記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}`);
您需要在遞歸調用之前添加“return”
return findContractStage(obj.children);
和
return findContractStage(obj[Object.keys(obj)[0]]);
因此,您的遞歸 function 會盡可能深入並返回您的 id 值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.