按值在深层嵌套对象中查找对象

时间:2019-09-30 12:17:28

标签: javascript

我想按值在深层嵌套对象中找到对象id。我尝试通过递归来做到这一点,但无法弄清楚为什么得到“未定义”。

在这段代码中,我在控制台"final result: 234324234"中是正确的,但是由于某些原因,此结果未从该函数返回。

请看看。

demo in 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}`);

3 个答案:

答案 0 :(得分:3)

您忘记return该功能

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}`);

答案 1 :(得分:2)

您没有返回递归时获得的值。试试这个:

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}`);

答案 2 :(得分:1)

您需要在递归调用之前添加“ return”

return findContractStage(obj.children);

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

因此,递归函数会更深入并返回id值。