[英]Structure an associative array for json
我正在嘗試在 function 中創建一個關聯數組。 function 從網站中提取信息。 我遇到的麻煩是創建 json 這將 output:
[{
Cardiology_STEMI : [
{name: Dr. John},
{service name: Cardiology STEMI}
{office: 973-111-1111},
{shift: 7a-7p},
{cell : 973-222-2222}]
}
{ Pulmonary : [
{name: Dr.Bob},
{office: (123)456-789},
{service_name: Pulmonary},
{shift: 7a-7p},
{cell: (123)456-789}
}]
[...and so on (x 30 entries])
我似乎無法弄清楚如何讓服務名稱顯示在主數組之外,以便它成為一個關聯數組。 當我嘗試修改數據 output 時,它永遠不會正確。
<!--Example of what the page looks like
<td class = "Cardiology specialty"> Cardiology STEMI </td>
<td class = "Cardiology specialty name"> Dr. John </td>
<td class = "Cardiology specialty shift"> 7a-7p </td>
<td class = "Cardiology specialty office"> (973)-111-1111 </td>
<td class = "Cardiology specialty cell"> (973)-222-2222 </td>
<td class = "Pulmonary specialty"> Pulmonary </td>
<td class = "Pulmonary specialty name"> Dr. Bob </td>
<td class = "Pulmonary specialty shift"> 7a-7p </td>
<td class = "Pulmonary specialty office"> (123)456-7890 </td>
<td class = "Pulmonary specialty cell"> (123)456-7890 </td>
-->
<script>
$(document).ready(function(){
var providers = []
var cardiology = data("Cardiology");
providers.push(cardiology);
var pulmonary = data("Pulmonary");
providers.push(pulmonary);
var json = JSON.stringify(providers);
sendData(json);
function data(specialty){
var service = $("."+specialty+".service:lt(1)").text().trim().replace(/\s/g,"_");
var service_name = $("."+specialty+".service:lt(1)").text();
var name = $("."+specialty+".name:lt(1)").text();
var shift = $("."+specialty+".shift:lt(1)").text();
var office = $("."+specialty+".office:lt(1)").text();
var cell = $("."+specialty+".cell:lt(1)").text();
var data = {
service: service_name,
name: name,
shift: shift,
office: office,
cell: cell};
return data
};
function sendData(data){
$.post('https://www.empa.app/api/submit_providers',
{ providers: data },
function(data, status, jqXHR) {
$('p').append('status: ' + status + ', data: ' + data);
})
}
})
</script>
任何人都可以幫助指導我到哪里我可能錯過了一些重要的東西,或者做錯了什么?
如果對於“關聯數組”,您的意思是“允許將數據存儲為key: value
對的結構”,是的,我們確實有。 在 JS 中,我們有Object
。
創建此類結構有多種方法,最常見的方法(也是 JS 開發人員首選和推薦的方法)是通過 object 文字{}
。 可以通過不同方式訪問此類結構中包含的數據; 預期的方式是點符號obj.key
,但類似數組的訪問器obj["key"]
是等效的:
var planet = {
name: "Earth",
greeting: "Hello, world!"
};
console.log("Planet", planet.name, "says:", planet["greeting"]);
// Planet Earth says: Hello, world!
有趣的事實:JS 中的Array
是Object
的特殊類型,其中每個key
都是 integer。 不過,我不會在這里詳細介紹 go,相反,我強烈建議您查閱 Mozilla 開發人員 JavaScript 參考,以了解有關 JS 基本結構的更多信息。
至於您的原始問題,您可以創建這樣的結構:
var list = {
Cardiology_STEMI: {
"Dr. John": {
office: "973-111-1111",
shift: "7a-7p",
cell: "973-222-2222"
},
"Dr. Mike": {
office: "973-333-3333",
shift: "4a-4p",
cell: "973-444-4444"
},
"Dr. Paul": {
office: "973-555-5555",
shift: "4a-4p",
cell: "973-666-6666"
}
},
Pulmonary: {
"Dr. Bob": {
office: "(123)456-789",
shift: "7a-7p",
cell: "(123)456-789"
},
"Dr. Bruce":{
office: "(987)654-321",
shift: "7a-7p",
cell: "(987)654-321"
},
"Dr. Banner":{
office: "(999)888-777",
shift: "7a-7p",
cell: "(999)888-777"
}
}
};
然后,使用類似數組的訪問器檢索特定醫生的數據:
var DrMike = list.Cardiology_STEMI["Dr. Mike"];
var DrBanner = list.Pulmonary["Dr. Banner"];
console.log(DrMike, DrBanner);
// {name: "Dr. Mike", office: "973-333-3333", shift: "4a-4p", cell: "973-444-4444"}
// {name: "Dr. Banner", office: "(999)888-777", shift: "7a-7p", cell: "(999)888-777"}
或者,一旦您更熟悉 Array 對象的filter()
、 map()
、 find()
(和其他)方法,實際的數組會更有益:
var list = {
Cardiology_STEMI: [
{
name: "Dr. John",
office: "973-111-1111",
shift: "7a-7p",
cell: "973-222-2222",
},{
name: "Dr. Mike",
office: "973-333-3333",
shift: "7a-7p",
cell: "973-444-4444",
},{
name: "Dr. Paul",
office: "973-555-5555",
shift: "7a-7p",
cell: "973-666-6666",
}
],
Pulmonary: [
{
name: "Dr. Bob",
office: "(123)456-789",
shift: "7a-7p",
cell: "(123)456-789",
},{
name: "Dr. Bruce",
office: "(987)654-321",
shift: "7a-7p",
cell: "(987)654-321",
},{
name: "Dr. Banner",
office: "(999)888-777",
shift: "7a-7p",
cell: "(999)888-777",
}
]
};
要查找特定類別中的特定醫生:
var DrMike = list.Cardiology_STEMI.find(drDetails=>{
return (drDetails.name === "Dr. Mike");
});
console.log(DrMike);
// {name: "Dr. Mike", office: "973-333-3333", shift: "4a-4p", cell: "973-444-4444"}
var DrBanner = list.Cardiology_STEMI.find(drDetails=>{
return (drDetails.name === "Dr. Banner");
});
console.log(DrBanner);
// {name: "Dr. Banner", office: "(999)888-777", shift: "7a-7p", cell: "(999)888-777"}
要查找在凌晨 4 點到下午 4 點之間可用的 heartology_STEMI 醫生:
var cardiology_STEMI_avail_4AM_4PM = list.Cardiology_STEMI.filter(drDetails => {
return (drDetails.shift === "4a-4p");
});
console.log(cardiology_STEMI_avail_4AM_4PM);
// [
// {name: "Dr. Mike", office: "973-333-3333", shift: "4a-4p", cell: "973-444-4444"}
// {name: "Dr. Paul", office: "973-555-5555", shift: "4a-4p", cell: "973-666-6666"}
// ]
希望這會有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.