projectb/checkParent.js
2023-10-13 15:25:50 +05:30

26 lines
1.0 KiB
JavaScript

function findParent(jsonData, targetId) {
function findParentChild(data, id, parentClasses, idKey) {
for (let i = 0; i < data.length; i++) {
const section = data[i];
if (section[idKey] === id) {
parentClasses.push(i);
// const childClassCount = (section.questions && section.questions.length > 0) ? section.questions.length : 0;
return parentClasses.length - 1
} else if (section.questions) {
const newParentClasses = parentClasses.slice(); // Create a shallow copy of parentClasses array
newParentClasses.push(i);
const childClass = findParentChild(section.questions, id, newParentClasses, idKey);
if (childClass) {
return childClass; // If childClass is found, return it
}
}
}
return null;
}
let result = findParentChild(jsonData.sections, targetId, [], "question_group_id");
return result;
}