At the time, I had just finished drawing the page when I received the request: at first I was confused, followed my thoughts, and then I was still confused. Maybe he was a little too big to write and then a colleague helped him with it. The requirements are as follows: Now I have fixed the quantity of goods and the number of employees, and distribute the goods to the selected people in turn. If there’s more stuff left after everyone’s done dividing up so many items that they give priority to the person at the top of the list. For example: 10 goods, 3 people, the final result is 1:1,4,7,10. Number two: 2,5,8. Number three: 3,6,9; I couldn’t do it at first, but the word “polling” was first mentioned to me by the back end. Most of the web is filled with Ajax requests and whatnot, and most of the articles come in a hundred copies without any clue. Although I understood the need, I didn’t know where to start. Finally, I was defeated by the strength of producing oil of my own dishes, and I handed it over to my colleagues to deal with it. After my colleagues dealt with it, I improved it and finally realized the effect. Now I’ve had my time, retraced it and realized it’s not that complicated. So let’s start coding.
// Start with a simple definition of products and employees
var goods = ["1"."2"."3"."4"."5"."6"."Seven"."8"."9"."10"];
var person = [
{
id: 1.good: []}, {id: 2.good: []}, {id: 3.good: []},];// Then we need to know how many times we can do this cycle, and the base at which each cycle starts
var counts = 0; // Can loop several times
var num = 1; // The cardinality of each loop
// If there are enough goods for each person to take one
if (goods.length % person.length == 0) {
counts = parseInt(goods.length / person.length);
} else {
// If it is not divisible enough, someone must have taken more
counts = parseInt(goods.length / person.length) + 1;
}
// We don't know exactly how many times, so we use the for loop
for (var index = 1; index <= counts; index++) {
// If the cycle is the first round, the base is 1, then the base is (number of people * (number of rounds -1))
if(index ! =1) {
num = (index - 1) * parseInt(person.length);
} else {
num = 1;
}
// Everyone goes round and round to get the corresponding goods
person.forEach((res, i) = > {
// It is the first round
if (index == num) {
res.good.push(goods[i])
} else {
// When an item exists, add it to the array
if (goods[i + num]) {
res.good.push(goods[i + num])
}
}
});
}
console.log(person);
Copy the code
Finally, I fulfilled this fancy demand (because I prefer dishes, casual for me some fancy, of course, for you big guy completely small case). This is my first post. I’m not good at it.