如何使用angular将对象列表转换为嵌套json

时间:2019-05-22 17:55:38

标签: javascript angular

这里我的数据如下

{
    "data": [
        {
            "_score": 0.5753642,
            "_type": "data",
            "_id": "coding-825x500_53a4ad23-69c6-42dc-bc4d-8",
            "_source": {
                "link": "coding-825x500_53a4ad23-69c6-42dc-bc4d-87da27a1b4ab",

                "information": {

                    "file_id": "coding-825x500_53a4ad23-69c6-42dc-bc4d-87da27a1b4ab",

                    "file_size": 67340,

                },

                 "dependent":"d1bc270d-3f7c-9927-f2f3-4de454b43305"
            },
            "_index": "brownfielddigitalization"
        },
        {
            "_score": 0.12222628,
            "_type": "data",
            "_id": "d1bc270d-3f7c-9927-f2f3-4de454b43305",
            "_source": {
                "link": "d1bc270d-3f7c-9927-f2f3-4de454b43305",

                "information": {

                    "file_id": "d1bc270d-3f7c-9927-f2f3-4de454b43305",

                    "file_size": 193,
                },
                "dependent":"info"

            },
            "_index": "brownfielddigitalization"
        },
        {
            "_score": 0.12222628,
            "_type": "data",
            "_id": "1_iwPLQjyFYRTVeQ2cb4S9rA_f13edad6-d252-4b2b-993a-96d143a4d910",
            "_source": {
                "link": "1_iwPLQjyFYRTVeQ2cb4S9rA_f13edad6-d252-4b2b-993a-96d143a4d910",

                "information": {

                    "file_id": "1_iwPLQjyFYRTVeQ2cb4S9rA_f13edad6-d252-4b2b-993a-96d143a4d910",

                    "file_size": 580969,

                },

                "dependent":"d1bc270d-3f7c-9927-f2f3-4de454b43305"
            },
            "_index": "brownfielddigitalization"
        },
        {
            "_score": 0.12222628,
            "_type": "data",
            "_id": "coding-825x500_59525fb2-87f8-45b9-9b16-ded6b78f66e9",
            "_source": {
                "link": "coding-825x500_59525fb2-87f8-45b9-9b16-ded6b78f66e9",

                "information": {

                    "artifact_id": "coding-825x500_59525fb2-87f8-45b9-9b16-ded6b78f66e9",

                    "file_size": 67340,

                },
               "dependent":null
            },
            "_index": "data"
        }
    ],
    "total": 4

}

在上面的数据中,我们具有“ 从属”键,分为3种类型 1.相关:信息 2.依存关系: 3.dependent:null

我的要求就像说我们有3个对象

{
     "id":"12",
     "dependent" : info  ->  Parent
   }
   {
     "id":"15",
     "dependent" : 12   --> child
   }
   {
     "id":"16",
     "dependent" : null    -> parent
   }

   {
     "id":"17",
     "dependent" : info  ->  Parent
   }
   {
     "id":"18",
     "dependent" : 17   --> child
   } 

如何将以上数据转换为父级子级/嵌套json格式

就像对象曾经具有“ dependent”:info 一样,在该对象的其余部分,我们必须检查“ dependent”:info id是否有id ==其他依赖于对象的id,如果相等,则它将成为依赖信息的子对象;如果曾经是依赖对象,则为null:那么它也必须是父对象,而不是子对象。所以可以这样做吗?

1 个答案:

答案 0 :(得分:0)

您可以通过以下编码来实现,

var sample = {
    "data": [
        {
            "_score": 0.5753642,
            "_type": "data",
            "_id": "coding-825x500_53a4ad23-69c6-42dc-bc4d-8",
            "_source": {
                "link": "coding-825x500_53a4ad23-69c6-42dc-bc4d-87da27a1b4ab",

                "information": {

                    "file_id": "coding-825x500_53a4ad23-69c6-42dc-bc4d-87da27a1b4ab",

                    "file_size": 67340,

                },

                 "dependent":"d1bc270d-3f7c-9927-f2f3-4de454b43305"
            },
            "_index": "brownfielddigitalization"
        },
        {
            "_score": 0.12222628,
            "_type": "data",
            "_id": "d1bc270d-3f7c-9927-f2f3-4de454b43305",
            "_source": {
                "link": "d1bc270d-3f7c-9927-f2f3-4de454b43305",

                "information": {

                    "file_id": "d1bc270d-3f7c-9927-f2f3-4de454b43305",

                    "file_size": 193,
                },
                "dependent":"info"

            },
            "_index": "brownfielddigitalization"
        },
        {
            "_score": 0.12222628,
            "_type": "data",
            "_id": "1_iwPLQjyFYRTVeQ2cb4S9rA_f13edad6-d252-4b2b-993a-96d143a4d910",
            "_source": {
                "link": "1_iwPLQjyFYRTVeQ2cb4S9rA_f13edad6-d252-4b2b-993a-96d143a4d910",

                "information": {

                    "file_id": "1_iwPLQjyFYRTVeQ2cb4S9rA_f13edad6-d252-4b2b-993a-96d143a4d910",

                    "file_size": 580969,

                },

                "dependent":"d1bc270d-3f7c-9927-f2f3-4de454b43305"
            },
            "_index": "brownfielddigitalization"
        },
        {
            "_score": 0.12222628,
            "_type": "data",
            "_id": "coding-825x500_59525fb2-87f8-45b9-9b16-ded6b78f66e9",
            "_source": {
                "link": "coding-825x500_59525fb2-87f8-45b9-9b16-ded6b78f66e9",

                "information": {

                    "artifact_id": "coding-825x500_59525fb2-87f8-45b9-9b16-ded6b78f66e9",

                    "file_size": 67340,

                },
               "dependent":null
            },
            "_index": "data"
        }
    ],
    "total": 4
};
var newArray = [];
for(i=0;i<sample.data.length;i++){
v = sample.data[i];
var obj = { "id":v._id, "dependent":v._source.dependent,"type":"Parent"};
if(v._source.dependent != null && v._source.dependent != "info" ){ obj.type = "child"; }
newArray.push(obj);
};
document.write(JSON.stringify(newArray));