46 lines
1.6 KiB
JavaScript
46 lines
1.6 KiB
JavaScript
|
|
function findParent(jsonData, targetId) {
|
|
function findParentChild(data, id, parentClasses, idKey,qid) {
|
|
for (let i = 0; i < data.length; i++) {
|
|
const section = data[i];
|
|
|
|
// console.log(section);
|
|
|
|
if (section[idKey] === id) {
|
|
parentClasses.push(i);
|
|
// const childClassCount = (section.questions && section.questions.length > 0) ? section.questions.length : 0;
|
|
// return parentClasses.length - 1, section.group_title
|
|
|
|
|
|
return {
|
|
value1: parentClasses.length - 1,
|
|
value2: section.group_title,
|
|
};
|
|
|
|
} else if (section.questions) {
|
|
|
|
// console.log(section.questions);
|
|
var a= section.questions
|
|
var qid = '';
|
|
a.forEach(element => {
|
|
qid = element.question_Id
|
|
});
|
|
// console.log(a.question_Id);
|
|
|
|
const newParentClasses = parentClasses.slice(); // Create a shallow copy of parentClasses array
|
|
newParentClasses.push(i);
|
|
const childClass = findParentChild(section.questions, id, newParentClasses, idKey,qid);
|
|
if (childClass) {
|
|
// console.log(childClass);
|
|
return childClass; // If childClass is found, return it
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
let result = findParentChild(jsonData.sections, targetId, [], "question_group_id");
|
|
return result;
|
|
}
|
|
|
|
|