0
var OBJ = {
    code: 42,
    item1: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        city: 'NY'
        name: 'bar'
    }],
    thing: [{
        id: 14,
        name: 'foo'
    }, {
        id: 5,
        street: 'E43'
        name: 'pub'
    }]
};

Javascript object(OBJ) I need a method that returns VALUE of KEY I pass as an argument if KEY is not present in OBJ method should return undefined

getKeyValueFromObject(OBJ , 'street') // should return 'E43'
getKeyValueFromObject(OBJ , 'dog') // should return undefined

I tried this(not working)

getKeyValueFromObject(obj: any, search: string) {
    const notFound = {};
    Object.keys(obj).forEach(key => {
      if (key !== null && key !== undefined && !(obj[key] === undefined || obj[key] === null)) {
        if (key === search) {
          return obj[key];
        } else if (obj[key].constructor === {}.constructor) {
          const result = this.getKeyValueFromObject(obj[key], search);
          if (result !== notFound) return result;
        } else if (Array.isArray(obj[key])) {
          obj[key].forEach(element => {
            const result = this.getKeyValueFromObject(element, search);
            if (result !== notFound) return result;
          });

        }
      }
    });
    return {};
  }
1

1 Answer 1

0

Your code was almost working, but the return statements in your forEach code were not working as you expected. Look here: Grab the return value and get out of forEach in JavaScript?. Instead I used a variable resultKey to store a match.

There were some commas missing in your OBJ json, but I think that was not the main problem.

var OBJ = {
    code: 42,
    item1: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        city: 'NY',
        name: 'bar'
    }],
    thing: [{
        id: 14,
        name: "foo"
    }, {
        id: 5,
        street: "E43",
        name: "pub"
    }]
};

function getKeyValueFromObject(obj, search) {
    let resultKey = undefined;
    const notFound = {};
    Object.keys(obj).forEach(key => {
      if (key !== null && key !== undefined && !(obj[key] === undefined || obj[key] === null)) {
        if (key === search) {
          resultKey = obj[key];
        } else if (obj[key].constructor === {}.constructor) {
          const result = this.getKeyValueFromObject(obj[key], search);
          if (result !== notFound) resultKey = result;
        } else if (Array.isArray(obj[key])) {
          obj[key].forEach(element => {
            const result = this.getKeyValueFromObject(element, search);
            if (result !== notFound) resultKey = result;
          });

        }
      }
      return;
    });
    return resultKey;
}
console.log(getKeyValueFromObject(OBJ, "street"));
console.log(getKeyValueFromObject(OBJ , "dog"));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.