javascript-将一个对象数组推入另一个对象失败

时间:2017-11-10 11:28:16

标签: javascript arrays

我有一系列庇护室。

 var rooms = [
    {
      "id": 1001,
      "room": "room 1"
    },
    {
      "id": 1002,
      "room": "room 2"
    },
    {
      "id": 1003,
      "room": "room 3"
    },
    {
      "id": 1004,
      "room": "room 4"
    }
  ];

我还有一份庇护患者名单。

 var patients = [
    {
        "id": 10,
        "room": "room 1",
        "patient_name": "John"
    },
    {
        "id": 11,
        "room": "room 1",
        "member_name": "Jane"
    },
    {
        "id": 12,
        "room": "room 1",
        "member_name": "Joe"
    },
    {
        "id": 20,
        "room": "room 2",
        "patient_name": "Matt"
    },
    {
        "id": 30,
        "room": "room 3",
        "patient_name": "Alexa"
    }
  ];

每位患者都属于特定的房间。我想在他们的房间下面添加这些患者并制作一个新的阵列,如下所示:

  var asylum = [
    {
      "id": 1001,
      "room": "room 1",
      "patients": [
        {
          "id": 10,
          "room": "room 1",
          "patient_name": "John"
        },
        {
          "id": 11,
          "room": "room 1",
          "member_name": "Jane"
        },
        {
          "id": 12,
          "room": "room 1",
          "member_name": "Joe"
        }
      ]
    },
    {
      "id": 1002,
      "room": "room 2",
      "patients": [
        {
          "id": 20,
          "room": "room 2",
          "patient_name": "Matt"
        }
      ]
    },
    {
      "id": 1003,
      "room": "room 3",
      "patients": [
        {
          "id": 30,
          "room": "room 3",
          "patient_name": "Alexa"
        }
      ]
    },
    {
      "id": 1004,
      "room": "room 4",
      "patients": []
    }
  ]

这是我的预期输出,但我并没有完全理解。这是我为实现预期结果而编写的代码。

for (var i = 0, len = rooms.length; i < len; i++) {
  for (var j = 0, len2 = patients.length; j < len2; j++) {
    if (rooms[i].room === patients[j].room) {
      rooms[i].members = patients[j];
    }
  }
}

我做了Fiddle。我在控制台中打印了数组。只有一个元素被推动。

4 个答案:

答案 0 :(得分:1)

每次找到病人时,您都会更改rooms[i].members的值。相反,您需要将新患者推入阵列:

for (var i = 0; i < rooms.length; i++) {
  for (var j = 0; j < patients.length; j++) {
    if (rooms[i].room === patients[j].room) {
        if (!rooms[i].members) { //this is the first patient found for this room, so you need to initialize the array
            rooms[i].members = [];
        }
        rooms[i].members.push(patients[j]);
    }
  }
}

答案 1 :(得分:1)

patients进行迭代,并为每位患者检查roomrooms。如果找到,请添加到找到的房间

var rooms = [{"id": 1001, "room": "room 1"}, {"id": 1002, "room": "room 2"}, {"id": 1003, "room": "room 3"}, {"id": 1004, "room": "room 4"}];

var patients = [{"id": 10, "room": "room 1", "patient_name": "John"}, { "id": 11, "room": "room 1", "member_name": "Jane"}, {"id": 12, "room": "room 1", "member_name": "Joe"}, {"id": 20, "room": "room 2", "patient_name": "Matt"}, {"id": 30, "room": "room 3", "patient_name": "Alexa"}];
  
patients.forEach(patient => {
    var room = rooms.find(room => patient.room === room.room);
    
    if(room) {
         
       if(!room.patients) {
          room.patients = [];
       }
      
       room.patients.push(patient);
    }

 });
 
 console.log(rooms);

答案 2 :(得分:1)

您可以通过生成庇护数组并分配哈希和第二个循环来将患者推入房间,从而为相同的房间使用哈希表,并使用双循环方法。

var rooms = [{ id: 1001, room: "room 1" }, { id: 1002, room: "room 2" }, { id: 1003, room: "room 3" }, { id: 1004, room: "room 4" }],
    patients = [{ id: 10, room: "room 1", patient_name: "John" }, { id: 11, room: "room 1", member_name: "Jane" }, { id: 12, room: "room 1", member_name: "Joe" }, { id: 20, room: "room 2", patient_name: "Matt" }, { id: 30, room: "room 3", patient_name: "Alexa" }],
    hash = Object.create(null),
    asylum = rooms.map(function (o) {
        return hash[o.room] = { id: o.id, room: o.room, patients: [] };
    });

patients.forEach(function (o) {
    hash[o.room].patients.push(o);
});

console.log(asylum);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:1)

这是我的脚本:请注意,有时使用patient_name和有时是member_name

会犯错误

&#13;
&#13;
    var asylum = [];


 var rooms = [
    {
      "id": 1001,
      "room": "room 1"
    },
    {
      "id": 1002,
      "room": "room 2"
    },
    {
      "id": 1003,
      "room": "room 3"
    },
    {
      "id": 1004,
      "room": "room 4"
    }
  ];

 var patients = [
    {
        "id": 10,
        "room": "room 1",
        "patient_name": "John"
    },
    {
        "id": 11,
        "room": "room 1",
        "patient_name": "Jane"
    },
    {
        "id": 12,
        "room": "room 1",
        "patient_name": "Joe"
    },
    {
        "id": 20,
        "room": "room 2",
        "patient_name": "Matt"
    },
    {
        "id": 30,
        "room": "room 3",
        "patient_name": "Alexa"
    }
  ];
  
  var asylum = [];
  
var aRoomWithPatients,apatient,rooma,roomb,name;
for (var i = 0; i < rooms.length; i++) {
   aRoomWithPatients = {};
   aRoomWithPatients.id = rooms[i].id;
   aRoomWithPatients.room = rooms[i].room;
   aRoomWithPatients.patients = [];
   asylum.push(aRoomWithPatients);
  for (var j = 0; j < patients.length;  j++) {
  rooma = rooms[i].room;
  roomb = patients[j].room;
  name = patients[j].patient_name;
    if (rooma === roomb) {
		apatient = {};
		apatient.id = patients[j].id;
		apatient.room = patients[j].room;
		apatient.patient_name = name;
        asylum[i].patients.push(apatient);
    }
  }
}
console.log(asylum);
&#13;
&#13;
&#13;