尝试从数组中的子对象创建数组

Posted

技术标签:

【中文标题】尝试从数组中的子对象创建数组【英文标题】:Trying to create array from child objects in array 【发布时间】:2019-09-18 14:13:36 【问题描述】:

我有一个需要删除的***数组,我正在使用 Angular7,但我不确定如何从该数组中提取子对象。我想摆脱“***”级别,这样您就可以拥有具有属性的对象。任何帮助将不胜感激。


    "toplevel": [
        
            "SetName": "name1",
            "description": "blah blah blah",
            "managingEntitySource": "blah blah blah",
            "effectiveDate": "01/01/9999",
            "terminationDate": "01/01/9999",
            "systemEffectiveDate": "01/01/9999",
            "systemTerminationDate": "01/01/9999",
            "comments": "blah blah blah",
            "lastProcessDate": "01/01/9999"
        ,
        
            "SetName": "name2",
            "description": "blah blah blah",
            "managingEntitySource": "blah blah blah",
            "effectiveDate": "01/01/9999",
            "terminationDate": "01/01/9999",
            "systemEffectiveDate": "01/01/9999",
            "systemTerminationDate": "01/01/9999",
            "comments": "blah blah blah",
            "lastProcessDate": "01/01/9999"
        
    ]

这是我正在寻找的东西:

[
        
            "SetName": "name1",
            "description": "blah blah blah",
            "managingEntitySource": "blah blah blah",
            "effectiveDate": "01/01/9999",
            "terminationDate": "01/01/9999",
            "systemEffectiveDate": "01/01/9999",
            "systemTerminationDate": "01/01/9999",
            "comments": "blah blah blah",
            "lastProcessDate": "01/01/9999"
        ,
        
            "SetName": "name2",
            "description": "blah blah blah",
            "managingEntitySource": "blah blah blah",
            "effectiveDate": "01/01/9999",
            "terminationDate": "01/01/9999",
            "systemEffectiveDate": "01/01/9999",
            "systemTerminationDate": "01/01/9999",
            "comments": "blah blah blah",
            "lastProcessDate": "01/01/9999"
        
    ]

【问题讨论】:

包括一个示例输出。不清楚“从这个数组中拉出子对象”是什么意思。 发布的输出示例 只访问对象属性? obj.topLevel 我遇到的问题是,当我尝试在 Angular 中进行查找时,我变得不确定,因为它没有在下一个级别查找。 【参考方案1】:

var jsonObject = 
    "toplevel": [
        
            "SetName": "name1",
            "description": "blah blah blah",
            "managingEntitySource": "blah blah blah",
            "effectiveDate": "01/01/9999",
            "terminationDate": "01/01/9999",
            "systemEffectiveDate": "01/01/9999",
            "systemTerminationDate": "01/01/9999",
            "comments": "blah blah blah",
            "lastProcessDate": "01/01/9999"
        ,
        
            "SetName": "name2",
            "description": "blah blah blah",
            "managingEntitySource": "blah blah blah",
            "effectiveDate": "01/01/9999",
            "terminationDate": "01/01/9999",
            "systemEffectiveDate": "01/01/9999",
            "systemTerminationDate": "01/01/9999",
            "comments": "blah blah blah",
            "lastProcessDate": "01/01/9999"
        
    ]
;

var innerArray = jsonObject.toplevel;

console.log(innerArray);

【讨论】:

以上是关于尝试从数组中的子对象创建数组的主要内容,如果未能解决你的问题,请参考以下文章

基于创建年份将对象的数组拆分为子数组

创建继承对象数组

如何在 C++ 中的子类中创建父对象数组?

从矩阵样式的对象数组创建数组数组

从 JavaScript 中的对象数组创建嵌套对象 [关闭]

从字符串数组创建对象