我知道有很多相似主题的问题,但我找不到任何问题的答案,所以我在这里写代码。 我有Json数据(如下所示),我想得到它的Days字段,并将它与另一个数组进行比较,如果值与某个任务匹配,则执行。
var DoctorsDetails={
"generalPractitioner":{
"drg001": {
"name":"Dr. Rajitha",
"age":"23",
"sex":"F",
"qualification":"MBBS",
"designation":"Duty Doctor",
"days":["Mon","Tue","Wed"],
"timef":"09:30 am",
"timet":"04:30 pm",
"slots":["1","2","3","4","5","6","7","8"]
},
"drg002": {
"name":"Dr. Bharadwaj",
"age":"23",
"sex":"M",
"qualification":"MBBS",
"designation":"Duty Doctor",
"days":["Thu","Fri","Sat"],
"timef":"10:30 am",
"timet":"02:30 pm",
"slots":["1","2","3","4","5","6","7","8"]
},
"drg003": {
"name":"Dr. Kotesh",
"age":"25",
"sex":"M",
"qualification":"MBBS",
"designation":"Duty Doctor",
"days":["Sun","Thu","Sat"],
"timef":"09:30 am",
"timet":"01:30 pm",
"slots":["1","2","3","4","5","6","7","8"]
}
},
"physician":{
"drp001":
{
"name":"Dr. Dwarkanath",
"age":"55",
"sex":"M",
"qualification":"MBBS, DIP, CAR",
"designation":"General Physician",
"days":["Mon","Tue","Wed","Thu","Fri","Sat"],
"timef":"11:00 am",
"timet":"05:30 pm",
"slots":["1","2","3","4","5","6","7","8"]
},
"drp002": {
"name":"Dr. Madhu Muddaiah",
"age":"35",
"sex":"M",
"qualification":"MBBS,MD",
"designation":"Consultant Physican",
"days":["Mon","Tue","Wed","Thu","Fri","Sat"],
"timef":"02:30pm",
"timet":"08:30pm",
"slots":["1","2","3","4","5","6","7","8"]
},
"drp003": {
"name":"Dr. Arvind",
"age":"35",
"sex":"M",
"qualification":"MBBS,MD",
"designation":"Consultant Physican",
"days":["Mon","Tue","Wed","Thu","Fri","Sat"],
"timef":"10:30 am",
"timet":"03:30 pm",
"slots":["1","2","3","4","5","6","7","8"]
}
},
};
console.log(DoctorsDetails["generalPractitioner"]deg001.days);
所以这只会给我drg001的日子,但我想要显示/捕获所有ID的日子。
为了获得每个id的日期,我必须编写哪些代码? 我知道我必须使用.each函数,但我如何自动获取这些ID名称。
答案 0 :(得分:1)
试试这个,你可以使用它来提取任何字段。
$.each(DoctorsDetails.generalPractitioner,function(i,v){
console.log(v.days);
});