// const objectScan = require('object-scan');
const search = (name, data) => objectScan(['**.name'], {
rtn: 'parents',
abort: true,
filterFn: ({ value }) => value === name
})(data);
const modules = [{ name: 'Module1', submodules: [{ name: 'Submodule1', id: 1, submodules: [{ name: 'Submodule11', id: 1, submodules: [] }, { name: 'Submodule12', id: 2, submodules: [{ name: 'Submodule121', id: 1, submodules: [] }, { name: 'Submodule122', id: 2, submodules: [] }] }] }, { name: 'Submodule2', id: 2, submodules: [] }] }, { name: 'Module2', submodules: [{ name: 'Submodule1', id: 3, submodules: [] }, { name: 'Submodule2', id: 4, submodules: [] }] }];
console.log(search('Submodule122', modules));
/* =>
[ { name: 'Submodule122', id: 2, submodules: [] },
[ { name: 'Submodule121', id: 1, submodules: [] },
{ name: 'Submodule122', id: 2, submodules: [] } ],
{ name: 'Submodule12',
id: 2,
submodules:
[ { name: 'Submodule121', id: 1, submodules: [] },
{ name: 'Submodule122', id: 2, submodules: [] } ] },
[ { name: 'Submodule11', id: 1, submodules: [] },
{ name: 'Submodule12',
id: 2,
submodules:
[ { name: 'Submodule121', id: 1, submodules: [] },
{ name: 'Submodule122', id: 2, submodules: [] } ] } ],
{ name: 'Submodule1',
id: 1,
submodules:
[ { name: 'Submodule11', id: 1, submodules: [] },
{ name: 'Submodule12',
id: 2,
submodules:
[ { name: 'Submodule121', id: 1, submodules: [] },
{ name: 'Submodule122', id: 2, submodules: [] } ] } ] },
[ { name: 'Submodule1',
id: 1,
submodules:
[ { name:...
// Output truncated for simplicity
console.log(search('unknown', modules));
// => undefined
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="533c31393630277e2030323d1362607d6b7d63">[email protected]</a>"></script>