Sergey
Sergey

Reputation: 1075

Find object in deep nested object by value

I would like to find an object id in deep nested object by value. 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.

Please, take a look.

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

Upvotes: 1

Views: 109

Answers (3)

Erailea
Erailea

Reputation: 78

You need to add "return" before recursive calls

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.

Upvotes: 1

Harun Yilmaz
Harun Yilmaz

Reputation: 8558

You forget to return the 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}`);

Upvotes: 3

Nick
Nick

Reputation: 147166

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

Upvotes: 2

Related Questions