[英]Accessing unique nested JSON array items
我試圖深入研究以下數組,以在解析完所有連接器類型項后將其變為選擇選項。
目前,我可以獲取所需的所有數據,但我想將返回的每個項目都轉換為單個字符串,而不是讓它返回具有多個連接器類型的項目作為單個項目(示例顯示以下內容)我目前有/渴望,並且只顯示唯一的類型,因此,如果連接器名稱已顯示一次,則不要再顯示該名稱作為選項。
例如(當前輸出):
(顯示重復項,類型不只一種的嵌套項不會分解成一行)
ConnectorType1
ConnectorType1, ConnectorType2, ConnectorType3
ConnectorType1
ConnectorType1, ConnectorType2
所需的輸出:
(僅顯示唯一項,所有結果都換行。)
ConnectorType1,
ConnectorType2,
ConnectorType3
Pastebin與JSON示例:
{
"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>`);
}
}
您可以選擇一個Set
並檢查該項目是否不在Set中,然后使用該項目並將其添加到Set中。
var array = [{ connector: ['ConnectorType1'] }, { connector: ['ConnectorType1', 'ConnectorType2', 'ConnectorType3'] }, { connector: ['ConnectorType1'] }, { connector: ['ConnectorType1', 'ConnectorType2'] }], connectors = new Set; array.forEach(({ connector }) => connector.forEach(c => { if (connectors.has(c)) return; console.log(c); connectors.add(c); }));
我建議您遍歷Array.from(myJson.ChargeDevice[0].Connector, ...
所有連接。
然后,對於每個連接,則push
的值.ConnectorType
到一個數組( myConnArr
),如果它不是已存在。 像這樣if(!myConnArr.includes(conn.ConnectorType)) myConnArr.push(conn.ConnectorType)
最后,我join
所有結果,並像.join(", \\n")
這樣將它們分開。
完整的代碼段。 為了進行測試,我復制了一些連接器值,以顯示remove_duplicates()
正常工作。
let myJson = { "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": "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": "\£5 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 } ] }; let myConnArr = []; Array.from(myJson.ChargeDevice[0].Connector, conn => { if(!myConnArr.includes(conn.ConnectorType)) myConnArr.push(conn.ConnectorType) }); console.log(myConnArr.join(", \\n"));
不知道這是您要執行的操作,但是這里有一個函數可以返回ChargeDevice和一些測試中的唯一連接器數組。
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)"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.