需要基于值将对象分组

时间:2019-04-08 16:16:35

标签: javascript

我在我的应用程序中使用React / Typescript,但需要有关基本JavaScript的帮助。我试图对来自api调用的数据进行分组(在本示例中,我将使用模拟数据),而不修改实际的JSON。我需要根据“ deviceType”值对数据进行分组。因此,如果有4个对象,如果两个是ipad,两个是iphone,则需要按顺序对它们进行分组。现在,模拟数据显示的是Ipad air,然后是Iphone 6plus,然后是Ipad air,然后是Iphone 6plus。我需要对它们进行分组,以便新对象首先具有两个ipadAir对象,然后再具有iphone 6plus对象。我相信可以通过使用传播运算符复制旧对象并在返回之前对其进行修改来完成。这是数据对象:

{
    "data": {
        "DirectvNowDeviceNotifications": [
            {
                "AccountNumber": "180802190357553",
                "AlertDescription": [
                    {
                        "AlertType": "BUFFERINGERRORS",
                        "AlertCode": "CRITICAL_ISFATAL",
                        "AlertInfo": "ACCOUNT_ALERT",
                        "SkipDismissal": false,
                        "DISMISSAL_EXPIRY_DATE": "",
                        "Content": {
                            "issueTitle": "Buffering Error Detected",
                            "issueDescription": "Buffering Error Detected"
                        },
                        "AlertAdditionalInfo": [
                            {
                                "Name": "reasonCodes",
                                "Value": "CRITICAL_ISFATAL"
                            },
                            {
                                "Name": "make",
                                "Value": "APPLE"
                            },
                            {
                                "Name": "deviceType",
                                "Value": "IPAD AIR"
                            },
                            {
                                "Name": "deviceID",
                                "Value": "F920BE29-1321-498A-B5D7-7FA84396DE16"
                            },
                            {
                                "Name": "osName",
                                "Value": "IOS"
                            },
                            {
                                "Name": "ISP",
                                "Value": "TIME WARNER CABLE INTERNET LLC"
                            },
                            {
                                "Name": "Mobile Carrier",
                                "Value": "AT&T"
                            },
                            {
                                "Name":"workflowName",
                                "Value":"CCE_ATTTV_Wifi_Setup"
                            }
                        ],
                        "AdditionalContent": {
                            "longDescription": "You received this error message because the system has captured buffering errors on your device while streaming."
                        }
                    },
                    {
                        "AlertType": "BUFFERINGERRORS",
                        "AlertCode": "CRITICAL_FATALV",
                        "AlertInfo": "ACCOUNT_ALERT",
                        "SkipDismissal": false,
                        "DISMISSAL_EXPIRY_DATE": "",
                        "Content": {
                            "issueTitle": "Buffering Error Detected",
                            "issueDescription": "Buffering Error Detected"
                        },
                        "AlertAdditionalInfo": [
                            {
                                "Name": "reasonCodes",
                                "Value": "CRITICAL_FATALVIDEOERROR_CT"
                            },
                            {
                                "Name": "make",
                                "Value": "APPLE"
                            },
                            {
                                "Name": "deviceType",
                                "Value": "IPHONE 6 PLUS"
                            },
                            {
                                "Name": "deviceID",
                                "Value": "F920BE29-1321-498A-B5D7-7FA84396DE16"
                            },
                            {
                                "Name": "osName",
                                "Value": "IOS"
                            },
                            {
                                "Name": "ISP",
                                "Value": "TIME WARNER CABLE INTERNET LLC"
                            },
                            {
                                "Name": "Mobile Carrier",
                                "Value": "AT&T"
                            },
                            {
                                "Name":"workflowName",
                                "Value":"CCE_DTVN_PLAYER_ERRORS"
                            },
                            {
                                "Name":"workflowName",
                                "Value":"CCE_DTVN_PLAYER_ERRORS"
                            }
                        ],
                        "AdditionalContent": {
                            "longDescription": "You received this error message because the system has captured buffering errors on your device while streaming"
                        }
                    },
                    {
                        "AlertType": "OSUNSUPPORTED",
                        "AlertCode": "N_NOT_SUPPORTED",
                        "AlertInfo": "ACCOUNT_ALERT",
                        "SkipDismissal": false,
                        "DISMISSAL_EXPIRY_DATE": "",
                        "Content": {
                            "issueTitle": "The device you are trying to use with DIRECTV NOW is not supported.",
                            "issueDescription": "The device you are trying to use with DIRECTV NOW is not supported."
                        },
                        "AlertAdditionalInfo": [
                            {
                                "Name": "reasonCodes",
                                "Value": "VSTB_UNSUPPORTED_DEVICE"
                            },
                            {
                                "Name": "make",
                                "Value": "APPLE"
                            },
                            {
                                "Name": "deviceType",
                                "Value": "IPAD AIR"
                            },
                            {
                                "Name": "deviceID",
                                "Value": "F920BE29-1321-498A-B5D7-7FA84396DE16"
                            },
                            {
                                "Name": "osName",
                                "Value": "IOS"
                            },
                            {
                                "Name": "ISP",
                                "Value": "TIME WARNER CABLE INTERNET LLC"
                            },
                            {
                                "Name": "Mobile Carrier",
                                "Value": "AT&T"
                            },
                            {
                                "Name":"workflowName",
                                "Value":"CCE_DTVN_PLAYER_ERRORS"
                            }
                        ],
                        "AdditionalContent": {
                            "longDescription": "<a target='_blank' href='https://www.att.com/esupport/article.html#!/directv-now/KM1200941'>Here</a> is a list of supported devices."
                        }
                    },
                    {
                        "AlertType": "NA",
                        "AlertCode": "NA",
                        "AlertInfo": "NA",
                        "SkipDismissal": false,
                        "DISMISSAL_EXPIRY_DATE": "",
                        "Content": {
                            "issueTitle": "tell me more title",
                            "issueDescription": "tell me more description"
                        },
                        "AlertAdditionalInfo": [
                            {
                                "Name": "reasonCodes",
                                "Value": "NA"
                            },
                            {
                                "Name": "make",
                                "Value": "APPLE"
                            },
                            {
                                "Name": "deviceType",
                                "Value": "IPHONE 6 PLUS"
                            },
                            {
                                "Name": "deviceID",
                                "Value": "FB2468CF-BF73-4DBC-9600-C61553BB759F"
                            },
                            {
                                "Name": "osName",
                                "Value": "IOS"
                            },
                            {
                                "Name": "ISP",
                                "Value": "TIME WARNER CABLE INTERNET LLC"
                            },
                            {
                                "Name": "Mobile Carrier",
                                "Value": "AT&T"
                            }
                        ],
                        "AdditionalContent": {
                            "longDescription": "tell me more long long long description"
                        }
                    }
                ]
            }
        ]
    },
    "content": {
        "code": 200,
        "message": "OK"
    }
}

需要根据设备类型对对象进行分组,并返回具有正确分组的新对象。如果有人能帮助我,我将非常感激。谢谢

1 个答案:

答案 0 :(得分:0)

您可以遍历对象的键,在下面的示例中,我假设您将其定义为javascript对象,结果将是两个对象,每个对象的键对应于设备的类型

const newGroup = [];
const data = object["data"]["DirectvNowDeviceNotifications"];
(data).forEach((map, index) => {
        Object(map['AlertDescription']).forEach((devices) => {
        if(typeof devices['AlertAdditionalInfo'] !== 'undefined'){
        const deviceType = devices['AlertAdditionalInfo'].find((type) => type.Name ==='deviceType');
        if(typeof deviceType.Value !== 'undefined'){
            if(typeof newGroup[deviceType.Value] === 'undefined'){
            newGroup[deviceType.Value] = [];
          }
            newGroup[deviceType.Value].push(devices);
        }
      }
    })
});

示例working

此代码只是您应该优化的示例,目的是让您对解决方案有所了解