0

I have an array of rooms of an asylum.

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

And I also have a list of patients of that asylum.

 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"
    }
  ];

Each patient belongs to a specific room. I wanna add those patients under their rooms and make a new array which looks like this:

  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": []
    }
  ]

That's my expected output but I'm not exactly getting that. This is the code I wrote to achieve the desired result.

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];
    }
  }
}

I made a Fiddle. I have printed the array in console. Only one element is getting pushed.

1
  • 1
    The last added patient overwrites the previous patient. Perhaps use an array and push the patients to it. Commented Nov 10, 2017 at 11:33

4 Answers 4

1

You are changing the value of rooms[i].members each time you find a patient. Instead, you need to push the new patient into the array:

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]);
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could use a hash table for the same rooms and a two loop approach by generating the asylum array and assigning the hash and the second loop for pushing the patients into the rooms.

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; }

Comments

1

Iterate over the patients and for each patient check the room with the rooms. If you find, add to the found room

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);

Comments

1

Here is my script : beware you made a mistake using sometimes patient_name and sometimes member_name

    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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.