如何将2个项目推送到数组中

4
在这个fiddle中,第27行显示了...
var doctors = [
    new Doctor("Doc A","A1"), 
    new Doctor("Doc B","A1"), 
    new Doctor("Doc C","A3")
];

这里,Doc ADoc BDoc C是硬编码的。它们也是 Knockout 绑定:

ko.applyBindings(
    new Patient("idValue", "nameValue", "addressValue", "Female", "usernameValue",
                "passwordValue", "emailValue", "mobileValue", "imgFileValue", 
                "imgSrcValue", "imagePathValue", "useridValue", "A3")
);

无论传递 a1a2 还是 a3,都会选择相应的文档。例如,我在绑定中传递了 a3,因此选择了 文档 C
给定此 JSON:
{
  "doctors": [
    {
      "id": 8,
      "schedules": [
        {
          "id": 8,
          "totime": "11:17",
          "dayId": 2,
          "location": "Somajiguda",
          "fromtime": "10:17",
          "hospitalId": 5,
          "day": "Tuesday",
          "hospital": "Yashoda"
        }
      ],
      "username": "d1",
      "degree": "DA(Anaesthesia)",
      "email": "1@2.com",
      "imagePath": "",
      "department": "Bio-Chemistry",
      "name": "d1",
      "userid": 51,
      "gender": "Male",
      "mobile": "1234567900"
    },
    {
      "id": 10,
      "schedules": [
        {
          "id": 10,
          "totime": "12:35",
          "dayId": 2,
          "location": "Somajiguda",
          "fromtime": "11:35",
          "hospitalId": 5,
          "day": "Tuesday",
          "hospital": "Yashoda"
        }
      ],
      "username": "d3",
      "degree": "BDS",
      "email": "d3@d3.com",
      "imagePath": "",
      "department": "Bio-Chemistry",
      "name": "d3",
      "userid": 56,
      "gender": "Male",
      "mobile": "1234567890"
    },
    {
      "id": 1,
      "schedules": [
        {
          "id": 1,
          "totime": "12:55",
          "dayId": 1,
          "location": "Somajiguda",
          "fromtime": "11:55",
          "hospitalId": 5,
          "day": "Monday",
          "hospital": "Yashoda"
        }
      ],
      "username": "doctor",
      "degree": "BDS",
      "email": "",
      "imagePath": null,
      "department": "Critical Care",
      "name": "doctor",
      "userid": 4,
      "gender": "Male",
      "mobile": "1234567890"
    },
    {
      "id": 7,
      "schedules": [
        {
          "id": 7,
          "totime": "11:17",
          "dayId": 2,
          "location": "Somajiguda",
          "fromtime": "11:17",
          "hospitalId": 5,
          "day": "Tuesday",
          "hospital": "Yashoda"
        }
      ],
      "username": "donald",
      "degree": "DA(Anaesthesia)",
      "email": "donald@doctor.com",
      "imagePath": "",
      "department": "Bio-Chemistry",
      "name": "donald",
      "userid": 47,
      "gender": "Male",
      "mobile": "1234567989"
    },
    {
      "id": 6,
      "schedules": [
        {
          "id": 6,
          "totime": "11:15",
          "dayId": 1,
          "location": "Somajiguda",
          "fromtime": "11:15",
          "hospitalId": 5,
          "day": "Monday",
          "hospital": "Yashoda"
        }
      ],
      "username": "john",
      "degree": "BDS",
      "email": "john@john.com",
      "imagePath": null,
      "department": "Anesthesiology",
      "name": "john",
      "userid": 46,
      "gender": "Male",
      "mobile": "1234567890"
    },
    {
      "id": 5,
      "schedules": [
        {
          "id": 5,
          "totime": "13:11",
          "dayId": 2,
          "location": "Somajiguda",
          "fromtime": "12:11",
          "hospitalId": 5,
          "day": "Tuesday",
          "hospital": "Yashoda"
        }
      ],
      "username": "sknayak",
      "degree": "BDS",
      "email": "sknayak@sknayak.com",
      "imagePath": "",
      "department": "Anesthesiology",
      "name": "sknayak",
      "userid": 38,
      "gender": "Male",
      "mobile": "1234567890"
    },
    {
      "id": 2,
      "schedules": [
        {
          "id": 2,
          "totime": "16:26",
          "dayId": 6,
          "location": "Somajiguda",
          "fromtime": "15:26",
          "hospitalId": 5,
          "day": "Saturday",
          "hospital": "Yashoda"
        }
      ],
      "username": "drsukant",
      "degree": "BDS",
      "email": "",
      "imagePath": null,
      "department": "Anesthesiology",
      "name": "sukant",
      "userid": 9,
      "gender": "Male",
      "mobile": "1234567890"
    }
  ]
}

使用GET请求获取:

$.ajax({
    type: "GET", 
    url: projectUrl+"getDoctors",  
    dataType:"json",
    jsonp: true,
    async:false ,
    success: function (data) {
        $.each(data.doctors, function(index, currPat) {
           console.log(currPat.username);
       });    
    }
});

我希望您能从JSON中获取用户名ID,并替换'Doc A','a1''Doc B','a2'等内容,以替换var doctors = ...。在我的AJAX成功函数中,我尝试了以下代码:
success: function (data) {
    $.each(data.doctors, function(index, currPat) {
        new Doctors(currPat.id,currPat.username);
    });    
}
3个回答

2
也许是这样的吗?
var doctors = [];

$.ajax({
type: "GET", 
url: projectUrl+"getDoctors",  
dataType:"json",
jsonp: true,
async:false ,
success: function (data) {
    $.each(data.doctors, function(index, currPat) {
       doctors.push[currPat.id,currPat.username];
   });    
}
});

你的最终结果应该像这样:
doctors = [[id1,name1],[id2,name2], ...... [id10,name10]]

你也可以使用这样的格式:

doctors.push({id:currPat.id,name:currPat.username});

那么你的最终结果应该是这样的:
doctors = [{id:id1,name:name1},{id:id2,name:name2},......,{id:id10,name:name10}]

*我个人更喜欢格式2

编辑 使用JQuery,.map()

var doctors;

$.ajax({
type: "GET", 
url: projectUrl+"getDoctors",  
dataType:"json",
jsonp: true,
async:false ,
success: function (data) {
    doctors = $.map(function () {
    return [data.id,data.username];}).get();    
}
});

比上面提到的稍微简单一些的写法。

谢谢回答,+1。我会检查你的方法。 - SpringLearner

1

看起来jQuery.map()可以实现你需要的功能...

success: function (data) {
    doctors = $.map(data.doctors, function(doctor) {
        new Doctor(doctor.id, doctor.username);
    });    
}

JSFiddle

的意思是在HTML页面中插入一个超链接,链接文本为“JSFiddle”。

哦,对了,我忘记了 .map() - jhyap
谢谢您的回答,+1。但是请问我应该在成功的ajax部分写什么? - SpringLearner

1
考虑你的Doctor函数长这样:

// Doctor constructor with name and username properties
var Doctor = function(nameParam, usernameParam){
    this.name = nameParam;
    this.username = usernameParam;
};

然后在 AJAX 成功函数中填充所有收到的医生,例如:

$.each(jsonStr.doctors, function(index, currPat) {
     var doc = new Doctor(currPat.name,currPat.username);
     doctors.push(doc);
 });
alert(JSON.stringify(doctors));

工作中 fiddle


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接