function createTree(arr, structure) {
const tree = Object.fromEntries(arr.map(n => [ n.id, { ...n, elements: [] } ]));
structure
.filter(n => n.parentId !== null)
.forEach(n => tree[n.parentId].elements.push(tree[n.id]));
return structure
.filter(n => n.parentId === null)
.map(n => tree[n.id]);
}
$('.accordion__grandchild__item').toggle()
должно было быть $('.accordion__grandchild__group').toggle()
. А вообще, по-хорошему, этот toggle (.accordion__child__group
это тоже касается) не нужен - дефолтное скрытое состояние можно задать через css.