1

I have a nested object as follows, and I want to find out the key name if value is given to me.

const a = {
  "key1": 12,
  "key2": {
    "nkey1": 123,
    "nkey2": 345
  }
};

const temp = (obj, val) => {
  return Object.keys(obj).find(key => !(typeof obj[key] === "object") ? obj[key] === val : temp(obj[key], val))
}

console.log(temp(a, 345));

I wrote the above piece of code. But it gives me output as key2 whereas I want output as nkey2.

What am I doing wrong?

1
  • typeof obj[key] === "object" ? obj[key] === val : temp(obj[key], val) - If obj[key] is an object, strictly-compare this object with val otherwise call temp(obj[key], val). You might want to re-think the order of operations... ;) Commented Aug 18, 2021 at 12:44

4 Answers 4

3

A function returns null if the value is not found, otherwise return the key.

const findKey = (obj, val) => {
  if (typeof obj === "object") {
    for (const key in obj) {
      if (obj[key] === val) {
        return key;
      } else {
        const result = findKey(obj[key], val);
        if (result !== null) return result;
      }
    }
  }

  return null;
};

const a = {
  key1: 12,
  key2: {
    nkey1: 123,
    nkey2: 345,
    nkey3: {
      dkey1: 232,
      dkey2: 777,
    },
  },
};

const output = findKey(a, 777);

console.log(output);

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

Comments

0

You can use for-in loop, to iterate over keys. If the value of that key matches val, you can return the key. Else, you can check if value is an object, and use recursion, to check if val matches any value of key in that object.

const a = {
  "key1": 12,
  "key2": {
    "nkey1": 123,
    "nkey2": 345
  }
};

const temp = (obj, val) => {
  for (let i in obj) {
    if(obj[i] === val){
      return i;
    } else if(typeof obj[i] === "object"){
       return temp(obj[i], val);
    }
  }
}

console.log(temp(a, 345));

Comments

0

Take a look at this:

const a = {
  "key1": 12,
  "key2": {
    "nkey1": 123,
    "nkey2": 345
  }
};

// recursive loop
const find = (obj, search = null) => {
  if (typeof obj === "object") {
    for (const key in obj) {                                         // loop over each property
      if (obj.hasOwnProperty(key) && obj[key] === search)            // if obj[key] is the value you are looking for (search) 
        return key;                                                  // return key
      else {                              
        let r = find(obj[key], search);                              // recursive call to find with the new object
        if (r !== null) return r;                                    // return key
      }
    }
  }
  return null;                                                       // default return value is null
}

console.log(12, find(a, 12));
console.log(123, find(a, 123));
console.log(345, find(a, 345));
console.log(0, find(a, 0));

3 Comments

"Take a look at this" - Is not a helpful answer. What is the problem with OPs version. What have you changed in your answer? And why did you do that? Or why is your answer better than fixing OPs version?
@Andreas, at least OP approach always return the key of first layer of the object because of the return Object.keys.find()
@BrattyNeal, this answer is not correct if the object have a data like this const a = { key1: { nkey1: 1, nkey2: 2, }, key2: { nkey3: 3, nkey4: 4, }, }; You can have a look of my answer.
-1

The find() function returns the first value of the the array for which the condition is true.

In the case of key === "key2" the recursive function is called which returns "nkey2" which results in the condition to be true and thats why "key2" is returned.

So again maybe to clear my way of thinking: You expect the find() function to return the result of a function call which is part of a condition. This wont work. The condition is true on key2 so find returns key2.

Hope this helps you out!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.