1

The following code prints the path of the key searched by its value inside an object:

function findKeyPath(obj, keyAttr, keyValue) {
 let path = "";
 (function findKey (obj, keyAttr, keyValue, keyPath) {
 const prevPath = `${keyPath ? keyPath + "." : ""}`;
 if (obj[keyAttr] === keyValue) {
 path = `${prevPath}${keyAttr}`;
 } else if (typeof obj === "object" && JSON.stringify(obj) === JSON.stringify(keyValue)) {
 path = prevPath.slice(0, -1); // remove last dot
 }
 if (typeof obj === "object" && !Array.isArray(obj)) {
 for (const key in obj) {
 if (Array.isArray(obj[key])) {
 for (let j = 0; j < obj[key].length; j++) {
 findKey(obj[key][j], keyAttr, keyValue, `${prevPath}${key}[${j}]`);
 }
 }
 if (obj[key] !== null && typeof obj[key] === "object") {
 findKey(obj[key], keyAttr, keyValue, `${prevPath}${key}`);
 }
 }
 }
 }(obj, keyAttr, keyValue));
 return path;
}
console.log(
 findKeyPath({
 users: [
 { name: "sam", surname: "red", age: 12 },
 { name: "sam", surname: "red", age: 42 },
 { name: "sam", surname: "red", age: 16 }
 ]
 }, "age", 16)
);

... it prints users[2].age.

Trying to taking out the inner function, like this:

function findKey(obj, keyAttr, keyValue, keyPath) {
 const prevPath = `${keyPath ? keyPath + "." : ""}`;
 if (obj[keyAttr] === keyValue) {
 return `${prevPath}${keyAttr}`;
 } else if (typeof obj === "object" && JSON.stringify(obj) === JSON.stringify(keyValue)) {
 return prevPath.slice(0, -1); // remove last dot
 }
 if (typeof obj === "object" && !Array.isArray(obj)) {
 for (const key in obj) {
 if (Array.isArray(obj[key])) {
 for (let j = 0; j < obj[key].length; j++) {
 findKey(obj[key][j], keyAttr, keyValue, `${prevPath}${key}[${j}]`);
 }
 }
 if (obj[key] !== null && typeof obj[key] === "object") {
 findKey(obj[key], keyAttr, keyValue, `${prevPath}${key}`);
 }
 }
 }
}
console.log(
 findKey({
 users: [
 { name: "sam", surname: "red", age: 12 },
 { name: "sam", surname: "red", age: 42 },
 { name: "sam", surname: "red", age: 16 }
 ]
 }, "age", 16)
);

It prints undefined. How can I solve it?

UPDATE, now it works:

function findKey(obj, keyAttr, keyValue, keyPath) {
 const prevPath = `${keyPath ? keyPath + "." : ""}`;
 let path = "";
 if (obj[keyAttr] === keyValue) {
 return `${prevPath}${keyAttr}`;
 } else if (typeof obj === "object" && JSON.stringify(obj) === JSON.stringify(keyValue)) {
 return prevPath.slice(0, -1); // remove last dot
 }
 if (typeof obj === "object" && !Array.isArray(obj)) {
 for (const key in obj) {
 if (Array.isArray(obj[key])) {
 for (let j = 0; j < obj[key].length; j++) {
 path = path.concat(findKey(obj[key][j], keyAttr, keyValue, `${prevPath}${key}[${j}]`));
 }
 }
 if (obj[key] !== null && typeof obj[key] === "object") {
 return path.concat(findKey(obj[key], keyAttr, keyValue, `${prevPath}${key}`));
 }
 }
 }
 return path;
}
General Grievance
5,12039 gold badges40 silver badges60 bronze badges
asked Sep 7, 2020 at 16:58

1 Answer 1

1

One problem with second code snippet I can think of is, we have path as a variable who is getting updated throughout the findKey function calls, it is a sort of "protected variable" which the findKey function can access and modify. In second code snippet we are not modifying the variable but returning it directly to the caller, which might not return anything for this part of code

if (typeof obj === "object" && !Array.isArray(obj)) {
for (const key in obj) {
 if (Array.isArray(obj[key])) {
 for (let j = 0; j < obj[key].length; j++) {
 findKey(obj[key][j], keyAttr, keyValue, `${prevPath}${key}[${j}]`);
 }
 }
 if (obj[key] !== null && typeof obj[key] === "object") {
 findKey(obj[key], keyAttr, keyValue, `${prevPath}${key}`);
 }
}

Imagine findKey getting called here and returning things as per your expectation but then we are not returning anything from this function, hence the undefined is getting returned. We might need to introduce a variable and store results and return it for this part of code.

answered Sep 7, 2020 at 18:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.