Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问唯一的嵌套JSON数组项_Javascript_Json - Fatal编程技术网

Javascript 访问唯一的嵌套JSON数组项

Javascript 访问唯一的嵌套JSON数组项,javascript,json,Javascript,Json,我试图深入研究以下数组,以便在解析完所有连接器类型项后将其转换为select选项 目前,我可以获得所需的所有数据,但我希望将返回的每个项目转换为单个字符串,而不是将具有多个连接器类型的项目作为单个项目返回(下面的示例显示了我当前拥有的/想要的。这只显示唯一的类型,因此,如果连接器名称已显示一次,则不要将该名称作为选项再次显示 例如(电流输出): (显示重复项,具有多个类型的嵌套项不会打断为单行) 期望输出: (仅显示唯一的项目,所有结果都拆分为新行) 带JSON的Pastebin示例: {

我试图深入研究以下数组,以便在解析完所有连接器类型项后将其转换为select选项

目前,我可以获得所需的所有数据,但我希望将返回的每个项目转换为单个字符串,而不是将具有多个连接器类型的项目作为单个项目返回(下面的示例显示了我当前拥有的/想要的。这只显示唯一的类型,因此,如果连接器名称已显示一次,则不要将该名称作为选项再次显示

例如(电流输出):

(显示重复项,具有多个类型的嵌套项不会打断为单行)

期望输出:

(仅显示唯一的项目,所有结果都拆分为新行)

带JSON的Pastebin示例:

{
    "ChargeDevice": [
        {
            "ChargeDeviceId": "cfeedcdd5e287bef4b583158a12363f1",
            "ChargeDeviceRef": "SRC_LDN60188",
            "ChargeDeviceName": "2 Riddons Road",
            "ChargeDeviceText": null,
            "ChargeDeviceLocation": {
                "Latitude": "51.431454",
                "Longitude": "0.031175",
                "Address": {
                    "SubBuildingName": null,
                    "BuildingName": "",
                    "BuildingNumber": "",
                    "Thoroughfare": "Riddons Road",
                    "Street": "Junction with Chinbrook Road",
                    "DoubleDependantLocality": null,
                    "DependantLocality": null,
                    "PostTown": "Leek",
                    "County": "Greater London",
                    "PostCode": "SE12 9QR",
                    "Country": "gb",
                    "UPRN": null
                },
                "LocationShortDescription": null,
                "LocationLongDescription": ""
            },
            "ChargeDeviceManufacturer": null,
            "ChargeDeviceModel": null,
            "PublishStatusID": "1",
            "DateCreated": "2014-08-19 05:15:02",
            "DateUpdated": "2015-09-02 11:28:16",
            "Attribution": "Source London",
            "DateDeleted": "n/a",
            "Connector": [
                {
                    "ConnectorId": "1",
                    "ConnectorType": "3-pin Type G (BS1363)",
                    "RatedOutputkW": "3.7",
                    "RatedOutputVoltage": "230",
                    "RatedOutputCurrent": "16",
                    "ChargeMethod": "Single Phase AC",
                    "ChargeMode": "1",
                    "ChargePointStatus": "In service",
                    "TetheredCable": "0",
                    "Information": "  x 3-pin square (BS 1363) - Standard (up to 3.7kW, 13-16A)",
                    "Validated": "0"
                },
                {
                    "ConnectorId": "2",
                    "ConnectorType": "Type 2 Mennekes (IEC62196)",
                    "RatedOutputkW": "7.0",
                    "RatedOutputVoltage": "230",
                    "RatedOutputCurrent": "32",
                    "ChargeMethod": "Single Phase AC",
                    "ChargeMode": "3",
                    "ChargePointStatus": "In service",
                    "TetheredCable": "0",
                    "Information": "  x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
                    "Validated": "0"
                }
            ],
            "DeviceOwner": {
                "OrganisationName": "Source London",
                "SchemeCode": "SRC_LDN",
                "Website": "https://www.sourcelondon.net",
                "TelephoneNo": "020 3056 8989"
            },
            "DeviceController": {
                "OrganisationName": "Source London",
                "SchemeCode": "SRC_LDN",
                "Website": "https://www.sourcelondon.net",
                "TelephoneNo": "020 3056 8989"
            },
            "DeviceAccess": [],
            "DeviceNetworks": "Source London",
            "ChargeDeviceStatus": "In service",
            "PublishStatus": "Published",
            "DeviceValidated": "0",
            "RecordModerated": "Y",
            "RecordLastUpdated": "2015-09-02 11:28:16",
            "RecordLastUpdatedBy": "NCR Admin",
            "PaymentRequiredFlag": false,
            "PaymentDetails": "",
            "SubscriptionRequiredFlag": true,
            "SubscriptionDetails": "\u00a35 per annum for RFiD card",
            "ParkingFeesFlag": false,
            "ParkingFeesDetails": "",
            "ParkingFeesUrl": null,
            "AccessRestrictionFlag": false,
            "AccessRestrictionDetails": "",
            "PhysicalRestrictionFlag": false,
            "PhysicalRestrictionText": "",
            "OnStreetFlag": true,
            "LocationType": "On-street",
            "Bearing": null,
            "Accessible24Hours": false
        }
    ]
}
通过JSON循环的当前代码:

for (let x = 0; x < data.ChargeDevice[i].Connector.length; x++) {
          if (connectors.indexOf(data.ChargeDevice[i].Connector[x].ConnectorType) === -1) {
            connectors.push(data.ChargeDevice[i].Connector[x].ConnectorType);
            $('#connectorList').append(`<option data-loc-name="${connectors}" value="${connectors}">${connectors}</option>`);
          }
        }
for(设x=0;x
不确定这是否是您想要的,但这里有一个函数,它返回充电器设备中的一组唯一连接器,并进行一点测试

function getUniqueConnectors(data) {
        var connectors = [];
        for (let i in data.ChargeDevice) {
            for (let x = 0; x < data.ChargeDevice[i].Connector.length; x++) {
                if (connectors.indexOf(data.ChargeDevice[i].Connector[x].ConnectorType) === -1) {
                    connectors.push(data.ChargeDevice[i].Connector[x].ConnectorType);
                }
            }
        }
        return connectors;
    }

    var objectOne = {
        "ChargeDevice": [
            {
                "ChargeDeviceId": "cfeedcdd5e287bef4b583158a12363f1",
                "ChargeDeviceRef": "SRC_LDN60188",
                "ChargeDeviceName": "2 Riddons Road",
                "ChargeDeviceText": null,
                "ChargeDeviceLocation": {
                    "Latitude": "51.431454",
                    "Longitude": "0.031175",
                    "Address": {
                        "SubBuildingName": null,
                        "BuildingName": "",
                        "BuildingNumber": "",
                        "Thoroughfare": "Riddons Road",
                        "Street": "Junction with Chinbrook Road",
                        "DoubleDependantLocality": null,
                        "DependantLocality": null,
                        "PostTown": "Leek",
                        "County": "Greater London",
                        "PostCode": "SE12 9QR",
                        "Country": "gb",
                        "UPRN": null
                    },
                    "LocationShortDescription": null,
                    "LocationLongDescription": ""
                },
                "ChargeDeviceManufacturer": null,
                "ChargeDeviceModel": null,
                "PublishStatusID": "1",
                "DateCreated": "2014-08-19 05:15:02",
                "DateUpdated": "2015-09-02 11:28:16",
                "Attribution": "Source London",
                "DateDeleted": "n/a",
                "Connector": [
                    {
                        "ConnectorId": "1",
                        "ConnectorType": "3-pin Type G (BS1363)",
                        "RatedOutputkW": "3.7",
                        "RatedOutputVoltage": "230",
                        "RatedOutputCurrent": "16",
                        "ChargeMethod": "Single Phase AC",
                        "ChargeMode": "1",
                        "ChargePointStatus": "In service",
                        "TetheredCable": "0",
                        "Information": "  x 3-pin square (BS 1363) - Standard (up to 3.7kW, 13-16A)",
                        "Validated": "0"
                    },
                    {
                        "ConnectorId": "2",
                        "ConnectorType": "Type 2 Mennekes (IEC62196)",
                        "RatedOutputkW": "7.0",
                        "RatedOutputVoltage": "230",
                        "RatedOutputCurrent": "32",
                        "ChargeMethod": "Single Phase AC",
                        "ChargeMode": "3",
                        "ChargePointStatus": "In service",
                        "TetheredCable": "0",
                        "Information": "  x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
                        "Validated": "0"
                    },
                    {
                        "ConnectorId": "2",
                        "ConnectorType": "Type 2 Mennekes (IEC62196)",
                        "RatedOutputkW": "7.0",
                        "RatedOutputVoltage": "230",
                        "RatedOutputCurrent": "32",
                        "ChargeMethod": "Single Phase AC",
                        "ChargeMode": "3",
                        "ChargePointStatus": "In service",
                        "TetheredCable": "0",
                        "Information": "  x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
                        "Validated": "0"
                    },
                    {
                        "ConnectorId": "2",
                        "ConnectorType": "Type 2 Mennekes (IEC62196)",
                        "RatedOutputkW": "7.0",
                        "RatedOutputVoltage": "230",
                        "RatedOutputCurrent": "32",
                        "ChargeMethod": "Single Phase AC",
                        "ChargeMode": "3",
                        "ChargePointStatus": "In service",
                        "TetheredCable": "0",
                        "Information": "  x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
                        "Validated": "0"
                    },
                ],
                "DeviceOwner": {
                    "OrganisationName": "Source London",
                    "SchemeCode": "SRC_LDN",
                    "Website": "https://www.sourcelondon.net",
                    "TelephoneNo": "020 3056 8989"
                },
                "DeviceController": {
                    "OrganisationName": "Source London",
                    "SchemeCode": "SRC_LDN",
                    "Website": "https://www.sourcelondon.net",
                    "TelephoneNo": "020 3056 8989"
                },
                "DeviceAccess": [],
                "DeviceNetworks": "Source London",
                "ChargeDeviceStatus": "In service",
                "PublishStatus": "Published",
                "DeviceValidated": "0",
                "RecordModerated": "Y",
                "RecordLastUpdated": "2015-09-02 11:28:16",
                "RecordLastUpdatedBy": "NCR Admin",
                "PaymentRequiredFlag": false,
                "PaymentDetails": "",
                "SubscriptionRequiredFlag": true,
                "SubscriptionDetails": "\u00a35 per annum for RFiD card",
                "ParkingFeesFlag": false,
                "ParkingFeesDetails": "",
                "ParkingFeesUrl": null,
                "AccessRestrictionFlag": false,
                "AccessRestrictionDetails": "",
                "PhysicalRestrictionFlag": false,
                "PhysicalRestrictionText": "",
                "OnStreetFlag": true,
                "LocationType": "On-street",
                "Bearing": null,
                "Accessible24Hours": false
            },
        ]
    };

    console.log(getUniqueConnectors(objectOne)); //["3-pin Type G (BS1363)", "Type 2 Mennekes (IEC62196)"]
函数getUniqueConnectors(数据){
变量连接器=[];
for(让我输入数据。ChargeDevice){
对于(设x=0;xfor (let x = 0; x < data.ChargeDevice[i].Connector.length; x++) {
          if (connectors.indexOf(data.ChargeDevice[i].Connector[x].ConnectorType) === -1) {
            connectors.push(data.ChargeDevice[i].Connector[x].ConnectorType);
            $('#connectorList').append(`<option data-loc-name="${connectors}" value="${connectors}">${connectors}</option>`);
          }
        }
function getUniqueConnectors(data) {
        var connectors = [];
        for (let i in data.ChargeDevice) {
            for (let x = 0; x < data.ChargeDevice[i].Connector.length; x++) {
                if (connectors.indexOf(data.ChargeDevice[i].Connector[x].ConnectorType) === -1) {
                    connectors.push(data.ChargeDevice[i].Connector[x].ConnectorType);
                }
            }
        }
        return connectors;
    }

    var objectOne = {
        "ChargeDevice": [
            {
                "ChargeDeviceId": "cfeedcdd5e287bef4b583158a12363f1",
                "ChargeDeviceRef": "SRC_LDN60188",
                "ChargeDeviceName": "2 Riddons Road",
                "ChargeDeviceText": null,
                "ChargeDeviceLocation": {
                    "Latitude": "51.431454",
                    "Longitude": "0.031175",
                    "Address": {
                        "SubBuildingName": null,
                        "BuildingName": "",
                        "BuildingNumber": "",
                        "Thoroughfare": "Riddons Road",
                        "Street": "Junction with Chinbrook Road",
                        "DoubleDependantLocality": null,
                        "DependantLocality": null,
                        "PostTown": "Leek",
                        "County": "Greater London",
                        "PostCode": "SE12 9QR",
                        "Country": "gb",
                        "UPRN": null
                    },
                    "LocationShortDescription": null,
                    "LocationLongDescription": ""
                },
                "ChargeDeviceManufacturer": null,
                "ChargeDeviceModel": null,
                "PublishStatusID": "1",
                "DateCreated": "2014-08-19 05:15:02",
                "DateUpdated": "2015-09-02 11:28:16",
                "Attribution": "Source London",
                "DateDeleted": "n/a",
                "Connector": [
                    {
                        "ConnectorId": "1",
                        "ConnectorType": "3-pin Type G (BS1363)",
                        "RatedOutputkW": "3.7",
                        "RatedOutputVoltage": "230",
                        "RatedOutputCurrent": "16",
                        "ChargeMethod": "Single Phase AC",
                        "ChargeMode": "1",
                        "ChargePointStatus": "In service",
                        "TetheredCable": "0",
                        "Information": "  x 3-pin square (BS 1363) - Standard (up to 3.7kW, 13-16A)",
                        "Validated": "0"
                    },
                    {
                        "ConnectorId": "2",
                        "ConnectorType": "Type 2 Mennekes (IEC62196)",
                        "RatedOutputkW": "7.0",
                        "RatedOutputVoltage": "230",
                        "RatedOutputCurrent": "32",
                        "ChargeMethod": "Single Phase AC",
                        "ChargeMode": "3",
                        "ChargePointStatus": "In service",
                        "TetheredCable": "0",
                        "Information": "  x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
                        "Validated": "0"
                    },
                    {
                        "ConnectorId": "2",
                        "ConnectorType": "Type 2 Mennekes (IEC62196)",
                        "RatedOutputkW": "7.0",
                        "RatedOutputVoltage": "230",
                        "RatedOutputCurrent": "32",
                        "ChargeMethod": "Single Phase AC",
                        "ChargeMode": "3",
                        "ChargePointStatus": "In service",
                        "TetheredCable": "0",
                        "Information": "  x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
                        "Validated": "0"
                    },
                    {
                        "ConnectorId": "2",
                        "ConnectorType": "Type 2 Mennekes (IEC62196)",
                        "RatedOutputkW": "7.0",
                        "RatedOutputVoltage": "230",
                        "RatedOutputCurrent": "32",
                        "ChargeMethod": "Single Phase AC",
                        "ChargeMode": "3",
                        "ChargePointStatus": "In service",
                        "TetheredCable": "0",
                        "Information": "  x 7-pin 'Smart' eg Mennekes (IEC 62196) - Fast (7kW, 32A)",
                        "Validated": "0"
                    },
                ],
                "DeviceOwner": {
                    "OrganisationName": "Source London",
                    "SchemeCode": "SRC_LDN",
                    "Website": "https://www.sourcelondon.net",
                    "TelephoneNo": "020 3056 8989"
                },
                "DeviceController": {
                    "OrganisationName": "Source London",
                    "SchemeCode": "SRC_LDN",
                    "Website": "https://www.sourcelondon.net",
                    "TelephoneNo": "020 3056 8989"
                },
                "DeviceAccess": [],
                "DeviceNetworks": "Source London",
                "ChargeDeviceStatus": "In service",
                "PublishStatus": "Published",
                "DeviceValidated": "0",
                "RecordModerated": "Y",
                "RecordLastUpdated": "2015-09-02 11:28:16",
                "RecordLastUpdatedBy": "NCR Admin",
                "PaymentRequiredFlag": false,
                "PaymentDetails": "",
                "SubscriptionRequiredFlag": true,
                "SubscriptionDetails": "\u00a35 per annum for RFiD card",
                "ParkingFeesFlag": false,
                "ParkingFeesDetails": "",
                "ParkingFeesUrl": null,
                "AccessRestrictionFlag": false,
                "AccessRestrictionDetails": "",
                "PhysicalRestrictionFlag": false,
                "PhysicalRestrictionText": "",
                "OnStreetFlag": true,
                "LocationType": "On-street",
                "Bearing": null,
                "Accessible24Hours": false
            },
        ]
    };

    console.log(getUniqueConnectors(objectOne)); //["3-pin Type G (BS1363)", "Type 2 Mennekes (IEC62196)"]