How to swap objects array index based on property [Edit]

This is a DIFFERENT PROBLEM than the one you originally describe.

Let me try asking you a question, so I can try and understand your logic. Why did you separate out the fourthYear in your final destination? Can the final chunks only have at most 2 people in them?

Does this accurately describe your requirements:

You must assign all students to groups.
There can be many groups.
Every group may have at most 2 people in it.
Every first year must be paired with a non-firstyear.

1 Like

Thank you for quickly replay @m_hutley

Switching logic i got my first success.
Maybe for now it is not elegant and modern, but work! :slight_smile:

function myFunction() {
var primoAnnoArr = [];
// primo = first
var altroAnnoArr = [];
//altro = other
var arrayT = [
  { student_id: "11", student_name: "Emanuele", student_year_id: "1" },
  { student_id: "6", student_name: "Alberto", student_year_id: "1" },
  { student_id: "14", student_name: "Simone", student_year_id: "1" },
  { student_id: "17", student_name: "Stefania", student_year_id: "1" },
  { student_id: "37", student_name: "Eleonora", student_year_id: "2" },
  { student_id: "31", student_name: "Antonella", student_year_id: "2" },
  { student_id: "45", student_name: "Francesco", student_year_id: "3" },
  { student_id: "69", student_name: "Eriselda", student_year_id: "4" },
  { student_id: "71", student_name: "Mariangela", student_year_id: "4" },
];
console.log(arrayT);

for( var i = 0; i < arrayT.length; i++){ 
    if(arrayT[i].student_year_id =="1")
    {
        primoAnnoArr.push(arrayT[i]); 
    }else
    { altroAnnoArr.push(arrayT[i])}

  
}

function getTiros(arr) {
  var shuffled = arr.sort(() => 0.5 - Math.random());
  return shuffled;        
}

// Fisher and Yates algorithm
function shuffle(array) {
   var tmp, current, top = array.length;
   if (top)
   while (--top) {
   current = Math.floor(Math.random() * (top + 1));
   tmp = array[current];
   array[current] = array[top];
   array[top] = tmp;
                  }
   return array;
 }


console.log(shuffle(primoAnnoArr));
// testing two different shuffle algo
console.log(getTiros(altroAnnoArr));
// testing two different shuffle algo
ground = [];

if (primoAnnoArr.length > altroAnnoArr.length) {
    console.log("primianni");
        for (let index = 0; index < primoAnnoArr.length; index++) {
            const element = primoAnnoArr[index];
            const element2 = altroAnnoArr[index];
            if (element | element2) {
                ground.push(element);
                ground.push(element2)
            }
            console.log(element);
            console.log(element2);
        }
       
}else {
    console.log("altrianni");
        for (let index = 0; index < altroAnnoArr.length; index++) {
            const element = altroAnnoArr[index];
            const element2 = primoAnnoArr[index];
            if (element != undefined) {
                ground.push(element);
            }
            if (element2 != undefined) {
                ground.push(element2)
            }
            console.log(element);
            console.log(element2);
        }
       
}
console.log("ground");
console.log(ground);

var arrayTc1=[];
var arrayTc2=[];
var arrayTcEmo=[];

for (var x =0; x < ground.length; x++){
     // console.log("cube[" + x + "][" + x + "] = " + cube[x]);
      if(x == 4 ){     
        arrayTc1.push(ground[0],ground[1],ground[2],ground[3]); 
      }
      if(x == 8 ){       
        arrayTc2.push(ground[4],ground[5],ground[6],ground[7]);
        arrayTcEmo.push(ground[8]);
      }

    }

console.log(arrayTcEmo);
console.log(arrayTc1);
console.log(arrayTc2);

}

myFunction();

For clarification:
imagine 3 rooms, where 2 of them need 2 student inside at morning and 2 two at evening.
Remaining one need only 1 in the morning. ← answer

I understand now this very interesting question. Actually no. There can be even more studenyi depending on the number of students assigned at the beginning. However, the last one in the figure houses only one as it is less important than the first two. Therefore, the first two most important classes cannot have two first year students for reasons of experience…

Based on the new information, I would separate out the first years, then pair them up with non-first years. It’s a simple solution that seems to meet all of the criteria.

2 Likes

Thank you! i Do that

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.