Being stuck for the whole day trying to find out the clue.
I have a three levels tree array of objects:
[
{
"id": 13,
"uuid": "spodvizhniki-uuid",
"parentUuid": null,
"name": "Сподвижники",
"slug": "spodvizhniki",
"order": 1,
"hasContent": false,
"level": 1,
"children": [
{
"id": 14,
"uuid": "sabit-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Сабит ибн Кайс Аль-Ансари ",
"slug": "sabit",
"order": 1,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 15,
"uuid": "khabbab-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Хаббаб ибн аль-Аратт",
"slug": "khabbab",
"order": 2,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 16,
"uuid": "suraka-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Сурака ибн Малик",
"slug": "suraka",
"order": 3,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 17,
"uuid": "rabi-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "ар-Раби бин Зияд аль-Хариси",
"slug": "rabi",
"order": 4,
"hasContent": true,
"level": 1,
"children": []
}
]
},
{
"id": 18,
"uuid": "spodvizhnicy-uuid",
"parentUuid": null,
"name": "Сподвижницы",
"slug": "spodvizhnicy",
"order": 5,
"hasContent": false,
"level": 1,
"children": [
{
"id": 19,
"uuid": "asma-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Асма бинт Абу Бакр | «Обладательница двух поясов",
"slug": "asma",
"order": 2,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 20,
"uuid": "rabia-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Рабиа ибн кааб",
"slug": "rabia",
"order": 3,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 21,
"uuid": "safiya-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Сафийа бинт Абдуль Мутталиб",
"slug": "safiya",
"order": 4,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 22,
"uuid": "zheny-proroka-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Жёны Пророка ﷺ",
"slug": "zheny-proroka",
"order": 5,
"hasContent": false,
"level": 1,
"children": [
{
"id": 23,
"uuid": "khadidzha-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Хадиджа бинт Хувайлид",
"slug": "khadidzha",
"order": 1,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 24,
"uuid": "aisha-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Аиша бинт Абу Бакр",
"slug": "aisha",
"order": 2,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 25,
"uuid": "sauda-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Сауда бинт Зама",
"slug": "sauda",
"order": 3,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 26,
"uuid": "ramlya-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Рамля бинт Абу Суфьян",
"slug": "ramlya",
"order": 4,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 27,
"uuid": "hafsa-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Хафса бинт Умар",
"slug": "hafsa",
"order": 5,
"hasContent": true,
"level": 1,
"children": []
}
]
}
]
}
]
And I want to add the hasThirdLevel: true property to the 2nd level objects if the children array is not empty. Tried a lot but could't find the clue.
Also the tree array was created from flat array:
[
{
"id": 13,
"uuid": "spodvizhniki-uuid",
"parentUuid": null,
"name": "Сподвижники",
"slug": "spodvizhniki",
"order": 1,
"hasContent": false
},
{
"id": 14,
"uuid": "sabit-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Сабит ибн Кайс Аль-Ансари ",
"slug": "sabit",
"order": 1,
"hasContent": true
},
{
"id": 23,
"uuid": "khadidzha-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Хадиджа бинт Хувайлид",
"slug": "khadidzha",
"order": 1,
"hasContent": true
},
{
"id": 19,
"uuid": "asma-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Асма бинт Абу Бакр | «Обладательница двух поясов",
"slug": "asma",
"order": 2,
"hasContent": true
},
{
"id": 15,
"uuid": "khabbab-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Хаббаб ибн аль-Аратт",
"slug": "khabbab",
"order": 2,
"hasContent": true
},
{
"id": 24,
"uuid": "aisha-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Аиша бинт Абу Бакр",
"slug": "aisha",
"order": 2,
"hasContent": true
},
{
"id": 20,
"uuid": "rabia-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Рабиа ибн кааб",
"slug": "rabia",
"order": 3,
"hasContent": true
},
{
"id": 16,
"uuid": "suraka-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Сурака ибн Малик",
"slug": "suraka",
"order": 3,
"hasContent": true
},
{
"id": 25,
"uuid": "sauda-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Сауда бинт Зама",
"slug": "sauda",
"order": 3,
"hasContent": true
},
{
"id": 17,
"uuid": "rabi-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "ар-Раби бин Зияд аль-Хариси",
"slug": "rabi",
"order": 4,
"hasContent": true
},
{
"id": 26,
"uuid": "ramlya-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Рамля бинт Абу Суфьян",
"slug": "ramlya",
"order": 4,
"hasContent": true
},
{
"id": 21,
"uuid": "safiya-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Сафийа бинт Абдуль Мутталиб",
"slug": "safiya",
"order": 4,
"hasContent": true
},
{
"id": 22,
"uuid": "zheny-proroka-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Жёны Пророка ﷺ",
"slug": "zheny-proroka",
"order": 5,
"hasContent": false
},
{
"id": 18,
"uuid": "spodvizhnicy-uuid",
"parentUuid": null,
"name": "Сподвижницы",
"slug": "spodvizhnicy",
"order": 5,
"hasContent": false
},
{
"id": 27,
"uuid": "hafsa-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Хафса бинт Умар",
"slug": "hafsa",
"order": 5,
"hasContent": true
}
]
CodePudding user response:
If I am understanding your question right you want the object with id 22 to have additional property 'hasThirdLevel: true' because its children is not empty?
for(let mainItem of arr) {
for(let secondItem of mainItem.children)
{
if(secondItem.children.length > 0)
{
secondItem["hasThirdLevel"]=true;
}
} }
Here is the code for that. If that's not it kindly paste the result you want in your question.
CodePudding user response:
Try this:
// Here's your tree array
const treeArray = [
{
"id": 13,
"uuid": "spodvizhniki-uuid",
"parentUuid": null,
"name": "Сподвижники",
"slug": "spodvizhniki",
"order": 1,
"hasContent": false,
"level": 1,
"children": [
{
"id": 14,
"uuid": "sabit-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Сабит ибн Кайс Аль-Ансари ",
"slug": "sabit",
"order": 1,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 15,
"uuid": "khabbab-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Хаббаб ибн аль-Аратт",
"slug": "khabbab",
"order": 2,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 16,
"uuid": "suraka-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "Сурака ибн Малик",
"slug": "suraka",
"order": 3,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 17,
"uuid": "rabi-uuid",
"parentUuid": "spodvizhniki-uuid",
"name": "ар-Раби бин Зияд аль-Хариси",
"slug": "rabi",
"order": 4,
"hasContent": true,
"level": 1,
"children": []
}
]
},
{
"id": 18,
"uuid": "spodvizhnicy-uuid",
"parentUuid": null,
"name": "Сподвижницы",
"slug": "spodvizhnicy",
"order": 5,
"hasContent": false,
"level": 1,
"children": [
{
"id": 19,
"uuid": "asma-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Асма бинт Абу Бакр | «Обладательница двух поясов",
"slug": "asma",
"order": 2,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 20,
"uuid": "rabia-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Рабиа ибн кааб",
"slug": "rabia",
"order": 3,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 21,
"uuid": "safiya-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Сафийа бинт Абдуль Мутталиб",
"slug": "safiya",
"order": 4,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 22,
"uuid": "zheny-proroka-uuid",
"parentUuid": "spodvizhnicy-uuid",
"name": "Жёны Пророка ﷺ",
"slug": "zheny-proroka",
"order": 5,
"hasContent": false,
"level": 1,
"children": [
{
"id": 23,
"uuid": "khadidzha-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Хадиджа бинт Хувайлид",
"slug": "khadidzha",
"order": 1,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 24,
"uuid": "aisha-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Аиша бинт Абу Бакр",
"slug": "aisha",
"order": 2,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 25,
"uuid": "sauda-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Сауда бинт Зама",
"slug": "sauda",
"order": 3,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 26,
"uuid": "ramlya-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Рамля бинт Абу Суфьян",
"slug": "ramlya",
"order": 4,
"hasContent": true,
"level": 1,
"children": []
},
{
"id": 27,
"uuid": "hafsa-uuid",
"parentUuid": "zheny-proroka-uuid",
"name": "Хафса бинт Умар",
"slug": "hafsa",
"order": 5,
"hasContent": true,
"level": 1,
"children": []
}
]
}
]
}
]
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// and here's a result which is the same as your tree array except the
// second level items have the extra property that indicates if they
// have children
const result = treeArray.map(firstLevelItem => ({
...firstLevelItem,
children: firstLevelItem.children.map(secondLevelItem=>({
...secondLevelItem,
children: secondLevelItem.children,
hasThirdLevel: !!secondLevelItem.children.length
}))
}))
result
Here's the result:
[
{
id: 13,
uuid: 'spodvizhniki-uuid',
parentUuid: null,
name: 'Сподвижники',
slug: 'spodvizhniki',
order: 1,
hasContent: false,
level: 1,
children: [
{
id: 14,
uuid: 'sabit-uuid',
parentUuid: 'spodvizhniki-uuid',
name: 'Сабит ибн Кайс Аль-Ансари ',
slug: 'sabit',
order: 1,
hasContent: true,
level: 1,
children: [],
hasThirdLevel: false
},
{
id: 15,
uuid: 'khabbab-uuid',
parentUuid: 'spodvizhniki-uuid',
name: 'Хаббаб ибн аль-Аратт',
slug: 'khabbab',
order: 2,
hasContent: true,
level: 1,
children: [],
hasThirdLevel: false
},
{
id: 16,
uuid: 'suraka-uuid',
parentUuid: 'spodvizhniki-uuid',
name: 'Сурака ибн Малик',
slug: 'suraka',
order: 3,
hasContent: true,
level: 1,
children: [],
hasThirdLevel: false
},
{
id: 17,
uuid: 'rabi-uuid',
parentUuid: 'spodvizhniki-uuid',
name: 'ар-Раби бин Зияд аль-Хариси',
slug: 'rabi',
order: 4,
hasContent: true,
level: 1,
children: [],
hasThirdLevel: false
}
]
},
{
id: 18,
uuid: 'spodvizhnicy-uuid',
parentUuid: null,
name: 'Сподвижницы',
slug: 'spodvizhnicy',
order: 5,
hasContent: false,
level: 1,
children: [
{
id: 19,
uuid: 'asma-uuid',
parentUuid: 'spodvizhnicy-uuid',
name: 'Асма бинт Абу Бакр | «Обладательница двух поясов',
slug: 'asma',
order: 2,
hasContent: true,
level: 1,
children: [],
hasThirdLevel: false
},
{
id: 20,
uuid: 'rabia-uuid',
parentUuid: 'spodvizhnicy-uuid',
name: 'Рабиа ибн кааб',
slug: 'rabia',
order: 3,
hasContent: true,
level: 1,
children: [],
hasThirdLevel: false
},
{
id: 21,
uuid: 'safiya-uuid',
parentUuid: 'spodvizhnicy-uuid',
name: 'Сафийа бинт Абдуль Мутталиб',
slug: 'safiya',
order: 4,
hasContent: true,
level: 1,
children: [],
hasThirdLevel: false
},
{
id: 22,
uuid: 'zheny-proroka-uuid',
parentUuid: 'spodvizhnicy-uuid',
name: 'Жёны Пророка ﷺ',
slug: 'zheny-proroka',
order: 5,
hasContent: false,
level: 1,
children: [
{
id: 23,
uuid: 'khadidzha-uuid',
parentUuid: 'zheny-proroka-uuid',
name: 'Хадиджа бинт Хувайлид',
slug: 'khadidzha',
order: 1,
hasContent: true,
level: 1,
children: []
},
{
id: 24,
uuid: 'aisha-uuid',
parentUuid: 'zheny-proroka-uuid',
name: 'Аиша бинт Абу Бакр',
slug: 'aisha',
order: 2,
hasContent: true,
level: 1,
children: []
},
{
id: 25,
uuid: 'sauda-uuid',
parentUuid: 'zheny-proroka-uuid',
name: 'Сауда бинт Зама',
slug: 'sauda',
order: 3,
hasContent: true,
level: 1,
children: []
},
{
id: 26,
uuid: 'ramlya-uuid',
parentUuid: 'zheny-proroka-uuid',
name: 'Рамля бинт Абу Суфьян',
slug: 'ramlya',
order: 4,
hasContent: true,
level: 1,
children: []
},
{
id: 27,
uuid: 'hafsa-uuid',
parentUuid: 'zheny-proroka-uuid',
name: 'Хафса бинт Умар',
slug: 'hafsa',
order: 5,
hasContent: true,
level: 1,
children: []
}
],
hasThirdLevel: true
}
]
}
]