Sobes.tech
Junior — Senior

Getting the property name of an object by its value

livecode

Task condition

Given an object. Implement a function that takes an object and a target value, and returns the name of the property corresponding to that value. If such a value is not present in the object, the function should return undefined or null.

function findKeyByValue(obj, target) {
    // your code
}

Example usage:

const data = { a: 1, b: 2, c: 3 };
console.log(findKeyByValue(data, 2)); // "b"
console.log(findKeyByValue(data, 5)); // undefined