JSFiddle - React, Tailwind, and code Playground
by Amit Sinha
JavaScript
function extractNameHref(obj) {
const result = [];
function traverse(node) {
if (node.hasOwnProperty("name") && node.hasOwnProperty("href")) {
result.push({
name: node.name,
href: node.href
});
}
if (node.hasOwnProperty("subMenuDTOList")) {
node.subMenuDTOList.forEach(subMenu => traverse(subMenu));
}
if (node.hasOwnProperty("sectionDTOList")) {
node.sectionDTOList.forEach(section => traverse(section));
}
if (node.hasOwnProperty("sortedSections")) {
node.sortedSections.forEach(sortedSection => traverse(sortedSection));
}
}
traverse(obj);
return result;
}
const originalData = [
// Your original JSON data here
];
const flatData = extractNameHref(originalData);
console.log(flatData);