Skip to content

Instantly share code, notes, and snippets.

@farhadjaman
Forked from asif-jalil/findNestedObject.js
Created June 29, 2023 05:48
Show Gist options
  • Save farhadjaman/ddca0084d431b12c9ba7b5c0cef0a549 to your computer and use it in GitHub Desktop.
Save farhadjaman/ddca0084d431b12c9ba7b5c0cef0a549 to your computer and use it in GitHub Desktop.
function findNestedObject(object, keyToFind, valToFind) {
let foundObj;
JSON.stringify(object, (_, nestedValue) => {
if (nestedValue && nestedValue[keyToFind] === valToFind) {
foundObj = nestedValue;
}
return nestedValue;
});
return foundObj;
}
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