const myarr = {
"id": 1,
"title": "Профильная часть",
"placeholder": "Профильная часть",
"type": "part",
"typeName": "profile",
"lectures": 0,
"practice": 6,
"homework": 0,
"content": [
{
"id": 2,
"title": "intermediate current",
"placeholder": "dadada",
"type": "module",
"lectures": 0,
"practice": 6,
"homework": 0,
"content": [
{
"id": 3,
"title": "intermediate current",
"placeholder": "ccccc",
"type": "theme",
"lectures": 0,
"practice": 6,
"homework": 0,
"content": [
{
"id": 4,
"title": "intermediate current",
"description": "ccccc",
"placeholder": "zxczcx",
"period": [
{
"id": 6,
"value": 6,
"title": "6 недели"
},
{
"id": 4,
"value": 4,
"title": "4 недели"
}
],
"workControl": [
"intermediate",
"current"
],
"lectures": 0,
"practice": 6,
"homework": 0,
"type": "practice",
"content": []
}
]
}
]
},
{
"id": 52,
"title": "intermediate current",
"placeholder": "dadada",
"type": "module",
"lectures": 0,
"practice": 6,
"homework": 0,
"content": [
{
"id": 53,
"title": "intermediate",
"placeholder": "ccccc",
"type": "theme",
"lectures": 0,
"practice": 6,
"homework": 0,
"content": [
{
"id": 54,
"title": "intermediate",
"description": "ccccc",
"placeholder": "zxczcx",
"period": [
{
"id": 56,
"value": 6,
"title": "6 недели"
},
{
"id": 54,
"value": 4,
"title": "4 недели"
}
],
"workControl": [
"intermediate"
],
"lectures": 0,
"practice": 6,
"homework": 0,
"type": "practice",
"content": []
}
]
}
]
},
]
'intermediate'
:[
{
"id":2,
"title":"intermediate current",
"placeholder":"dadada",
"type":"module",
"lectures":0,
"practice":6,
"homework":0,
"content":[
{
"id":3,
"title":"intermediate current",
"placeholder":"ccccc",
"type":"theme",
"lectures":0,
"practice":6,
"homework":0,
"content":[
{
"id":4,
"title":"intermediate current",
"description":"ccccc",
"placeholder":"zxczcx",
"period":[
{
"id":6,
"value":6,
"title":"6 недели"
},
{
"id":4,
"value":4,
"title":"4 недели"
}
],
"workControl":[
"intermediate",
"current"
],
"lectures":0,
"practice":6,
"homework":0,
"type":"practice",
"content":[
]
}
]
}
]
},
{
"id":52,
"title":"intermediate current",
"placeholder":"dadada",
"type":"module",
"lectures":0,
"practice":6,
"homework":0,
"content":[
{
"id":53,
"title":"intermediate",
"placeholder":"ccccc",
"type":"theme",
"lectures":0,
"practice":6,
"homework":0,
"content":[
{
"id":54,
"title":"intermediate",
"description":"ccccc",
"placeholder":"zxczcx",
"period":[
{
"id":56,
"value":6,
"title":"6 недели"
},
{
"id":54,
"value":4,
"title":"4 недели"
}
],
"workControl":[
"intermediate"
],
"lectures":0,
"practice":6,
"homework":0,
"type":"practice",
"content":[
]
}
]
}
]
}
]
let arr = [];
let currentControl = myarr.map(part => {
part.content.filter(elem => {
if(elem.content.length){
console.log('elee', elem.title)
elem.content.filter(elem2 => {
if(elem2.content.length){
console.log('el2', elem2)
elem2.content.filter(elem3 => {
console.log('el3', elem3)
if(elem3.workControl.filter(control => {
console.log('control', control)
if(control == 'intermediate'){
arr.push(elem)
}
}))
return []
})
return []
}
})
return []
}
})
});
document.getElementById("dd").prepend(JSON.stringify(arr))
const isValid = n => n.workControl?.includes('intermediate');
const getFromTree = (tree, childrenKey, test) =>
Array.isArray(tree)
? tree.reduce((acc, n) => (
test(n) && acc.push(n),
acc.push(...getFromTree(n[childrenKey], childrenKey, test)),
acc
), [])
: [];
const result = getFromTree(tree, 'content', isValid);
function* getNestedData(data, test) {
if (test(data)) {
yield data;
}
if (data instanceof Object) {
for (const k in data) if (Object.hasOwn(data, k)) {
yield* getNestedData(data[k], test);
}
}
}
const result = [...getNestedData(tree, isValid)];
function getNestedData(data, test) {
const result = [];
for (const stack = [ data ]; stack.length;) {
const n = stack.pop();
if (test(n)) {
result.push(n);
}
if (n instanceof Object) {
stack.push(...Object.values(n).reverse());
}
}
return result;
}
const getFromTree = function*(tree, childrenKey, test) {
const stack = [];
for (let [ i, arr ] = this(tree); ++i < arr.length || stack.length;) {
if (i === arr.length) {
[ i, arr ] = stack.pop();
} else {
if (test(arr[i])) {
yield arr[i];
}
stack.push([ i, arr ]);
[ i, arr ] = this(arr[i][childrenKey]);
}
}
}.bind(x => [ -1, x instanceof Array ? x : [] ]);
function getIntermediateItems(tree, result = []) {
tree?.forEach((item) => {
if (item.workControl?.includes('intermediate')) {
result.push(item);
}
getIntermediateItems(item.content, result);
});
return result;
}
const arr = getIntermediateItems(myarr);
document.getElementById("dd").prepend(JSON.stringify(arr, '', 4))