将属性添加到多级数组的第二层对象
整天都被困在试图找出线索。
我有三个级别的对象树数组:
[
{
"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": []
}
]
}
]
}
]
如果孩子数组不为空,则将hasthirdlevel:true属性添加到第二级对象。尝试了很多,但找不到线索。
另外,树阵列是由平面阵列创建的:
[
{
"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
}
]
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
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解您的问题,您希望具有ID 22的对象具有其他属性“ hasthirdlevel:true”,因为它的孩子不是空的?
这是为此的代码。如果不是这样,那就粘贴了您想要的结果。
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?
Here is the code for that. If that's not it kindly paste the result you want in your question.
尝试以下操作:
这是结果:
Try this:
Here's the result:
您可以简单地通过迭代和检查第三级儿童阵列长度的长度来实现这一目标。
演示:
You can simply achieve that by iterating and checking the length of 3rd level children array length.
Demo :