SOURCE

const dat = [
    {
        "mdmId": "dev_12915b2b-b505-46ac-9c53-9201e909dfc0",
        "name": "世纪阳光集团",
        "sn": "20191108",
        "disable": null,
        "top": true,
        "description": "世纪阳光集团",
        "location": [
            "118.860939",
            "36.743942"
        ],
        "dlocation": null,
        "type": "object",
        "parentId": null,
        "customerId": "sjyg",
        "properties": {
            "type": "company"
        },
        "updateTime": 1573102367767,
        "createTime": 1569582006340,
        "deviceType": null,
        "children": [
            {
                "mdmId": "dev_13e73aa0-05e7-4059-82fc-f2557e5cd55d",
                "name": "华迈纸业",
                "sn": null,
                "disable": null,
                "top": false,
                "description": "华迈",
                "location": [
                    "118.840939",
                    "36.726962"
                ],
                "dlocation": null,
                "type": "object",
                "parentId": "dev_12915b2b-b505-46ac-9c53-9201e909dfc0",
                "customerId": "sjyg",
                "properties": {
                    "type": "area"
                },
                "updateTime": 1573089301522,
                "createTime": 1573019404723,
                "deviceType": null
            },
            {
                "mdmId": "dev_cc093830-000a-4437-becc-40a1804dd339",
                "name": "科迈纸业",
                "sn": null,
                "disable": null,
                "top": false,
                "description": "科迈",
                "location": [
                    "118.840000",
                    "36.726962"
                ],
                "dlocation": null,
                "type": "object",
                "parentId": "dev_12915b2b-b505-46ac-9c53-9201e909dfc0",
                "customerId": "sjyg",
                "properties": {
                    "type": "area"
                },
                "updateTime": 1573089373413,
                "createTime": 1573019532787,
                "deviceType": null
            },
            {
                "mdmId": "dev_8041edbe-4e6a-42cc-985f-ed22d03cd6ae",
                "name": "盛世热电",
                "sn": null,
                "disable": null,
                "top": false,
                "description": "自备电厂",
                "location": [
                    "118.860939",
                    "36.733942"
                ],
                "dlocation": null,
                "type": "object",
                "parentId": "dev_12915b2b-b505-46ac-9c53-9201e909dfc0",
                "customerId": "sjyg",
                "properties": {
                    "type": "area"
                },
                "updateTime": 1573097999584,
                "createTime": 1573026520840,
                "deviceType": null,
                "children": [
                    {
                        "mdmId": "dev_e46fa8bf-887f-4296-8a4a-d4035266475c",
                        "name": "6_炉1号风机电机",
                        "sn": "SunshinePaper_PP002A",
                        "disable": false,
                        "top": false,
                        "description": "无线振动传感器",
                        "location": [
                            "118.858278",
                            "36.746401"
                        ],
                        "dlocation": null,
                        "type": "primitive",
                        "parentId": "dev_8041edbe-4e6a-42cc-985f-ed22d03cd6ae",
                        "customerId": "sjyg",
                        "properties": {
                            "lastFixDate": 1573915077264
                        },
                        "updateTime": 1574325915541,
                        "createTime": 1572244080812,
                        "deviceType": null,
                        "templates": [
                            {
                                "target": {
                                    "mdmId": "tpl_b690c8eb-bf0f-499f-bd47-2d15b8d41325",
                                    "name": "Vibration_batch_data",
                                    "customerId": "sjyg",
                                    "protocolId": "ptc_a8d8a565-38c9-4356-803e-6d999f1ee933",
                                    "description": "振动传感器批量数据获取",
                                    "properties": {},
                                    "createTime": 1582614864800,
                                    "updateTime": null,
                                    "protocol": null
                                },
                                "properties": {
                                    "devId": "9c65f9fffea90001",
                                    "rtuId": "9c65f9fffea90001",
                                    "mdmId": "tpl_b690c8eb-bf0f-499f-bd47-2d15b8d41325"
                                }
                            },
                            {
                                "target": {
                                    "mdmId": "tpl_01a3176f-6a35-4706-a549-7d79b7e69b7d",
                                    "name": "振动传感器",
                                    "customerId": "sjyg",
                                    "protocolId": "ptc_a8d8a565-38c9-4356-803e-6d999f1ee933",
                                    "description": "振动传感器",
                                    "properties": {},
                                    "createTime": 1569584933825,
                                    "updateTime": null,
                                    "protocol": null
                                },
                                "properties": {
                                    "devId": "9c65f9fffea90001",
                                    "rtuId": "9c65f9fffea90001",
                                    "mdmId": "tpl_01a3176f-6a35-4706-a549-7d79b7e69b7d"
                                }
                            }
                        ],
                        "connected": true
                    },
                    {
                        "mdmId": "dev_2efa0966-abdc-49aa-be02-276452ef0196",
                        "name": "6_炉1号风机轴承",
                        "sn": "SunshinePaper_PP001A",
                        "disable": false,
                        "top": false,
                        "description": "无线振动传感器",
                        "location": [
                            "118.858278",
                            "36.746401"
                        ],
                        "dlocation": null,
                        "type": "primitive",
                        "parentId": "dev_8041edbe-4e6a-42cc-985f-ed22d03cd6ae",
                        "customerId": "sjyg",
                        "properties": {
                            "lastFixDate": 1573052225839
                        },
                        "updateTime": 1574325941361,
                        "createTime": 1569586025097,
                        "deviceType": null,
                        "templates": [
                            {
                                "target": {
                                    "mdmId": "tpl_b690c8eb-bf0f-499f-bd47-2d15b8d41325",
                                    "name": "Vibration_batch_data",
                                    "customerId": "sjyg",
                                    "protocolId": "ptc_a8d8a565-38c9-4356-803e-6d999f1ee933",
                                    "description": "振动传感器批量数据获取",
                                    "properties": {},
                                    "createTime": 1582614864800,
                                    "updateTime": null,
                                    "protocol": null
                                },
                                "properties": {
                                    "devId": "9c65f9fffea90002",
                                    "rtuId": "9c65f9fffea90002",
                                    "mdmId": "tpl_b690c8eb-bf0f-499f-bd47-2d15b8d41325"
                                }
                            },
                            {
                                "target": {
                                    "mdmId": "tpl_01a3176f-6a35-4706-a549-7d79b7e69b7d",
                                    "name": "振动传感器",
                                    "customerId": "sjyg",
                                    "protocolId": "ptc_a8d8a565-38c9-4356-803e-6d999f1ee933",
                                    "description": "振动传感器",
                                    "properties": {},
                                    "createTime": 1569584933825,
                                    "updateTime": null,
                                    "protocol": null
                                },
                                "properties": {
                                    "devId": "9c65f9fffea90002",
                                    "rtuId": "9c65f9fffea90002",
                                    "mdmId": "tpl_01a3176f-6a35-4706-a549-7d79b7e69b7d"
                                }
                            }
                        ],
                        "connected": true
                    }
                ]
            },
            {
                "mdmId": "dev_10082e01-fb23-43f6-8369-1f92c7c26d6c",
                "name": "振动传感器Test",
                "sn": null,
                "disable": false,
                "top": false,
                "description": null,
                "location": [],
                "dlocation": null,
                "type": "primitive",
                "parentId": "dev_12915b2b-b505-46ac-9c53-9201e909dfc0",
                "customerId": "sjyg",
                "properties": {},
                "updateTime": 1582614961933,
                "createTime": 1582614961933,
                "deviceType": null,
                "templates": [
                    {
                        "target": {
                            "mdmId": "tpl_01a3176f-6a35-4706-a549-7d79b7e69b7d",
                            "name": "振动传感器",
                            "customerId": "sjyg",
                            "protocolId": "ptc_a8d8a565-38c9-4356-803e-6d999f1ee933",
                            "description": "振动传感器",
                            "properties": {},
                            "createTime": 1569584933825,
                            "updateTime": null,
                            "protocol": null
                        },
                        "properties": {
                            "devId": "9c65f9fffea90009",
                            "rtuId": "9c65f9fffea90009",
                            "mdmId": "tpl_01a3176f-6a35-4706-a549-7d79b7e69b7d"
                        }
                    },
                    {
                        "target": {
                            "mdmId": "tpl_b690c8eb-bf0f-499f-bd47-2d15b8d41325",
                            "name": "Vibration_batch_data",
                            "customerId": "sjyg",
                            "protocolId": "ptc_a8d8a565-38c9-4356-803e-6d999f1ee933",
                            "description": "振动传感器批量数据获取",
                            "properties": {},
                            "createTime": 1582614864800,
                            "updateTime": null,
                            "protocol": null
                        },
                        "properties": {
                            "devId": "9c65f9fffea90009",
                            "rtuId": "9c65f9fffea90009",
                            "mdmId": "tpl_b690c8eb-bf0f-499f-bd47-2d15b8d41325"
                        }
                    }
                ],
                "connected": false
            },
            {
                "mdmId": "dev_b63d5c54-7ed0-498e-9954-1b260b7d6d29",
                "name": "text",
                "sn": null,
                "disable": true,
                "top": false,
                "description": null,
                "location": [],
                "dlocation": null,
                "type": "primitive",
                "parentId": "dev_12915b2b-b505-46ac-9c53-9201e909dfc0",
                "customerId": "sjyg",
                "properties": {},
                "updateTime": 1608638207341,
                "createTime": 1608638207341,
                "deviceType": "device",
                "templates": [
                    {
                        "target": {
                            "mdmId": "tpl_01a3176f-6a35-4706-a549-7d79b7e69b7d",
                            "name": "振动传感器",
                            "customerId": "sjyg",
                            "protocolId": "ptc_a8d8a565-38c9-4356-803e-6d999f1ee933",
                            "description": "振动传感器",
                            "properties": {},
                            "createTime": 1569584933825,
                            "updateTime": null,
                            "protocol": null
                        },
                        "properties": {
                            "devId": "sss",
                            "rtuId": "sss",
                            "mdmId": "tpl_01a3176f-6a35-4706-a549-7d79b7e69b7d"
                        }
                    }
                ],
                "connected": false
            }
        ]
    }
]
let rdata = {
    name: '',
    templates: [],
    type: '',
    children: []
};
let d = []
d = (function ps(data) {

    let dict = []

    let templates = []
    if (data instanceof Array&&data[0].target!==undefined) {
        for (let k of data) {
            if ((k.target !== undefined && k.target.name !== undefined)) {
                // 此处是template :array
                templates.push(k['target']['name'])
            }
        }
        return templates
    }
    for (let i of data) {
        let obj = {}
        // 此处是dict
        obj.name = i.name

        console.log(i.name)
        if (i.templates !== undefined) {
            obj['templates'] = ps(i.templates)
        }
        if (i.children === undefined) {
            dict.push(obj)
        } else {
            obj.children = ps(i.children)
            dict.push(obj)
        }
    }


    return dict
})
(dat);
console.log(d)
console 命令行工具 X clear

                    
>
console