Exercise 17: Find Key by Value

Problem Statement

Function `findKey(obj, value)` banao jo object me value ke corresponding key return kare. Example: findKey({a: 1, b: 2, c: 1}, 1) => "a" (first match)

Sample Output:

findKey({a: 1, b: 2, c: 1}, 1) => "a"
findKey({x: "hello"}, "hello") => "x"

Solution

const findKey = (obj, value) => {
  return Object.keys(obj).find(key => obj[key] === value);
};

Explanation

Overall Goal:

  • Value ke corresponding key find karna.

Real world:

  • Reverse lookup: value to key.
  • Data search: value-based search.