-
-
Save farhadjaman/ddca0084d431b12c9ba7b5c0cef0a549 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findNestedObject(object, keyToFind, valToFind) { | |
let foundObj; | |
JSON.stringify(object, (_, nestedValue) => { | |
if (nestedValue && nestedValue[keyToFind] === valToFind) { | |
foundObj = nestedValue; | |
} | |
return nestedValue; | |
}); | |
return foundObj; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const object = { | |
schemaVersion: 15, | |
body: { | |
id: "cnWNPmSTDG", | |
rows: [ | |
{ | |
id: "tyrtnyEeRX", | |
columns: [ | |
{ | |
id: "oswmB3BZoE", | |
contents: [ | |
{ | |
id: "L_YoZhqJ7L", | |
type: "heading", | |
values: { | |
fontSize: "22px", | |
linkStyle: { | |
linkColor: "#0000ee" | |
}, | |
displayCondition: null | |
} | |
}, | |
{ | |
id: "XHWYbiN6Su", | |
type: "form", | |
values: { | |
action: { | |
method: "POST", | |
url: "https://mailbluster.com" | |
}, | |
fields: [ | |
{ | |
name: "email", | |
label: "Email address", | |
type: "email", | |
required: true | |
}, | |
{ | |
type: "checkbox", | |
name: "consent", | |
label: "Consent", | |
required: true | |
} | |
] | |
} | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
} | |
findNestedObject(object, "type", "checkbox"); | |
// expected output | |
// | |
// { | |
// type: 'checkbox', | |
// name: 'consent', | |
// label: 'Consent', | |
// options: 'I agree to your terms and condition', | |
// placeholder_text: 'Consent', | |
// show_label: false, | |
// required: true, | |
// meta_data: {} | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment