gpt4 book ai didi

javascript - 如何确定 JSON 对象是否有子项

转载 作者:行者123 更新时间:2023-11-28 00:44:39 25 4
gpt4 key购买 nike

我有一个通过一些 JSON 在 Angular 中构建的菜单,该菜单最多有 3 层深度。有些菜单项没有子级,而其他菜单项则有第二级或第三级。我正在尝试找出一种方法来确定选定的子类别是否有任何子元素,如果有,那么我需要隐藏一个按钮。

每次选择子类别(例如“数字艺术”)时,都会在 $scope.selectedSubMarket 中捕获该值;

如何在 $scope.art 中检查带有 cat: Digital Art 的对象是否有任何子对象?在 JSON 的更下方,您将看到像“漫画”这样的子类别,它没有 2 级,我需要一种方法来检查它,但我没有运气......

enter image description here

以下是构建菜单的 JSON 示例:

       $scope.art = [
{"cat": "Digital Art", "second": [
{"sub": "3 Dimensional Art", "third": [
{"desc": "Abstract"},
{"desc": "Characters"},
{"desc": "Objects"},
{"desc": "Scenes"},
{"desc": "Vehicles"},

]},
{"sub": "Animation", "third": [
{"desc": "Abstract"},
{"desc": "Characters"},
{"desc": "Animals"},
{"desc": "Fantasy"},
{"desc": "Graffiti"},
{"desc": "Illustrations"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-fi"},
{"desc": "Space Art"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Still Life"},
{"desc": "Surreal"},
]},
{"sub": "Fractal Art", "third": [
{"desc": "Fractal Animation"},
{"desc": "Fractal Manipulation"},
{"desc": "Raw Fractal"},
]},
{"sub": "Photomanipulation", "third": [
{"desc": "Abstract"},
{"desc": "Animals & Plants"},
{"desc": "Conceptual"},
{"desc": "Dark"},
{"desc": "Emotional"},
{"desc": "Humourous"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-fi"},
{"desc": "Minimalism"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Surreal"},
]},
{"sub": "Pixel Art"},
{"sub": "Text Art"},
{"sub": "Typography"},
{"sub": "Vector", "third": [
{"desc": "Abstract"},
{"desc": "Animals & Plants"},
{"desc": "Conceptual"},
{"desc": "Dark"},
{"desc": "Emotional"},
{"desc": "Humourous"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-fi"},
{"desc": "Minimalism"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Surreal"},
]},
{"sub": "Vexel", "third": [
{"desc": "Abstract"},
{"desc": "Animals & Plants"},
{"desc": "Conceptual"},
{"desc": "Dark"},
{"desc": "Emotional"},
{"desc": "Humourous"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-fi"},
{"desc": "Minimalism"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Surreal"},
]},


]},
{"cat": "Traditional Art", "second": [
{"sub": "Assemblage"},
{"sub": "Body Art", "third": [
{"desc": "Body Modification"},
{"desc": "Cosmetic Application"},
{"desc": "Tattoos"},
{"desc": "Scenes"},
{"desc": "Vehicles"},
]},
{"sub": "Collage"},
{"sub": "Drawings", "third": [
{"desc": "Abstract"},
{"desc": "Technical Drawing"},
{"desc": "Animals"},
{"desc": "Fantasy"},
{"desc": "Portrait & Figures"},
{"desc": "Illustrations"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-Fi"},
{"desc": "Space Art"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Still Life"},
{"desc": "Surreal"},

]},
{"sub": "Installation Art"},
{"sub": "Scratchboard"},
{"sub": "Paintings", "third": [
{"desc": "Abstract"},
{"desc": "Surreal"},
{"desc": "Animals"},
{"desc": "Fantasy"},
{"desc": "Portrait & Figures"},
{"desc": "Illustrations"},
{"desc": "Landscapes & Scenery"},
{"desc": "Macabre & Horror"},
{"desc": "People"},
{"desc": "Sci-Fi"},
{"desc": "Space Art"},
{"desc": "Political"},
{"desc": "Pop Art"},
{"desc": "Psychedelic"},
{"desc": "Still Life"},
{"desc": "Surreal"},
]},
{"sub": "Printing", "third": [
{"desc": "Collagraph"},
{"desc": "Dry Point"},
{"desc": "Etching"},
{"desc": "Lino"},
{"desc": "Lithograph"},
{"desc": "Monotype"},
{"desc": "Silk Screen"},
{"desc": "Wood Block"},
]},
{"sub": "Street Art", "third": [
{"desc": "Freehand Graffiti"},
{"desc": "Installations"},
{"desc": "Sketch Design"},
{"desc": "Stencils"},
{"desc": "Stickers"},
{"desc": "Wheatpasting"},
]},
{"sub": "Sculpture", "third": [
{"desc": "Abstract"},
{"desc": "Busts(People)"},
{"desc": "Fantasy"},
{"desc": "Figurative"},
{"desc": "Horror"},
{"desc": "Lego"},
{"desc": "Models"},
{"desc": "Surreal"},
]},

]},
{"cat": "Literature"},
{"cat": "Artisan Craft"},
{"cat": "Cartoons"},
{"cat": "Comics"},
{"cat": "Animation"},
{"cat": "Manga & Anime"},
{"cat": "Scraps"},
{"cat": "Anthro"},
{"cat": "Fan Art"},

]

最佳答案

如果我猜对了

var element = $scope.art.filter(function (el) {return el["cat"] === "Digital Art";})[0];
if (element.second && element.second.length) {
// Do something
};

对于子元素,请使用之前代码中的element:

var subElement = element["second"].filter(function (el) {return el["sub"] === "Animation";})[0];
if (subElement.third && subElement.third.length) {
// Do something
};

关于javascript - 如何确定 JSON 对象是否有子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27538321/

25 4 0
文章推荐: c++ - "this"是否也适配函数指针?
文章推荐: html - 简单的 5 星,但想要响应
文章推荐: c++ - 清除输入流
文章推荐: html - 使用 元素在 pdf 预览中显示的元素在 Chrome 中不是交互式的