- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个通过一些 JSON 在 Angular 中构建的菜单,该菜单最多有 3 层深度。有些菜单项没有子级,而其他菜单项则有第二级或第三级。我正在尝试找出一种方法来确定选定的子类别是否有任何子元素,如果有,那么我需要隐藏一个按钮。
每次选择子类别(例如“数字艺术”)时,都会在 $scope.selectedSubMarket 中捕获该值;
如何在 $scope.art 中检查带有 cat: Digital Art 的对象是否有任何子对象?在 JSON 的更下方,您将看到像“漫画”这样的子类别,它没有 2 级,我需要一种方法来检查它,但我没有运气......
以下是构建菜单的 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/
我正在尝试将 margin-left:20px 分配给表单内的所有 div,其类包含编辑,具有以下内容: form.edit > div { margin-left:20px; } 我的想法
我有这个 xpath: .//*[@id='some_id']/td//div 现在我想选择特定类型的 div 的任何子项,例如每个子项是标签或跨度。像这样的东西 .//*[@id='some_id'
我有一个包含包含用户信息的键列表的键,现在当我的表单加载时,我想将这些键作为数组获取。我该怎么做?我找到了获取计数的方法,但仍然不知道如何获取这些 key 。 最佳答案 您可以使用 Microsoft
关闭。这个问题需要更多 focused 。它目前不接受答案。 想要改进这个问题?更新问题,使其只关注 editing this post 的一个问题。 关闭 6 年前。 Improve this q
我正在通过一些在线教程来学习 AEM。根据教程,创建组件时,需要在 Allowed Parents 或 Allowed Children 中输入一些值。但是,我在窗口中看不到这样的选项。当我尝试创建组
我需要将 MDI 子窗体的创建集中到 Delphi (VCL) 中的一个独特过程中。这个想法是每次创建 MDI 子窗体时执行一些操作,无论其类型如何,即将其标题名称添加到列表中以访问该 MDI 子窗体
我试图在 TreeView 中获取所选节点的所有子节点,但遇到了一些问题。 以这个 TreeView 为例: 我想将所有子节点变为黄色突出显示的“文件夹”节点,这将是旁边有一条蓝线的子节点。 这是我尝
我在最小化我所有的 MDIChildren 时遇到了麻烦,遇到了 MDIChild to minimize not activated properly 我最小化所有 child 的代码是: proc
我使用下面的代码通过单击系统关闭按钮来关闭 MDI 子窗体,它工作正常: procedure Tfrm_main.FormClose(Sender: TObject; var Action: TC
仅当我指定对象的完整路径时,我才能通过指定特定子键来查找 Firebase 对象。这是为什么? 这有效 ref.child(`users/${user.uid}/watchlist/${key}`)
每当我单击工具栏菜单时,它每次都会显示新表单。我想阻止它一次又一次地显示相同的表单。在给出的代码中,form2 一次又一次地显示。我想停止它,以便它显示一次。 喜欢: private void new
我想知道是否有一种方法可以通过遍历父节点的 vector 来获取子节点中的数据。我有一个我计划经常更改的 XML 文件,因此我想避免对属性名称进行硬编码。因此,我想在我的子节点中提取数据而不使用 pt
假设我有以下 YAML 文件: - key1: value # and so on... key99: value key100: subkey1: value # an
我不是代码天才,而是行动脚本爱好者。 你能帮我吗? 我有一个函数,根据选择的对象,该函数将事件监听器调用已经在舞台上的一组“子项目”(我想在单击时重新使用具有更改参数的子项目,而不是创建多个实例和代码
我需要一些帮助来查询分层数据。这是一个简单的表,其中 parent_id 引用 id 并且对于根条目可能为 null。 create table edition ( id
我尝试获得一个简单的 GEF 编辑器。我有一个 GraphicalEditorWithPalette 来创建我的示例模型。我有一个覆盖 createFigure 和 getModelChildren
我正在尝试搜索其中包含子项(文本区域)的表格单元格。我努力了td.children.value,td.childNodes.value,td.firstChild.value,td.lastChild
我有一个 mdi 父 form 并且我在运行时通过以下代码将我的其他 form 作为 mdi 子窗体打开: private void MenuItem_Click(object sender, Eve
我在 Activity 中加载了一个 GridView,其中存在 fragment 。 GridView 本身并不位于 Fragment 中。我通过 BaseAdapter 创建了一个客户适配器,一切
我在导航 Controller 中有两个 child (根 child 和第二个 child )。我通常先去找根 child ,然后再去找第二个 child 。这允许我使用导航 Controller
我是一名优秀的程序员,十分优秀!