简体   繁体   English

通过循环将值分配给数组索引

[英]Assign value to array indexes through loop

I am struggling to find apply the loop function to an array and I was hoping you could help me. 我正在努力寻找将循环函数应用于数组的方法,希望您能为我提供帮助。 I have a group list and I want to automatically assign people requests to groups and assign these to areas where there are vacancies. 我有一个组列表,我想自动将人员请求分配到组,并将这些请求分配到有空缺的区域。
Therefore im looking at a table where I would get : 因此,即时通讯看着我会得到一个表:
Group 1 | 第1组| NB People NB人
Group 2 | 第2组| NB People NB人
Group 3 | 第3组| NB People NB人
and so on... 等等...

I therefore know I need an "if" condition to check compare people request with vacancies in each group and then assign it if vacancy in group is higher than reservation request. 因此,我知道我需要一个“如果”条件来检查比较人员请求与每个组中的空缺,然后如果组中的空缺高于预订请求,则将其分配。
I know how to add the reservation variable to an array with the array.push function but I am struggling to loop and assign to an array containing 2 columns... 我知道如何使用array.push函数将保留变量添加到数组中,但是我正在努力循环并分配给包含2列的数组...

    <label for="groups">Reservation request (1-10):</label>
    <div id="reservation">
    <input type="number" name="form" id="number" min="1" max='10'>
    <br><br>
    <button onclick="groups()">Submit</button>
    </div>



 var myArray = ["Group 1", "Group 2", "Group 3"];
 function groups(){
 form = number.value;
 if (form >=1){  
     for (var i; i<myArray.length; i++) {
         if (myArray[i] > form){
             myArray[i] = form;
             console.log(myArray);
         }
         else {alert("no");}

Can you help me out please? 你能帮我吗?

Many thanks Marion 非常感谢Marion

If i understand you correctly, what you want to do is: 如果我正确理解您的意思,您想做的是:

  • start with a list of some empty groups, who can grow when adding people, but never grow above 10 members 从一些空组开始,这些空组在添加人员时可以成长,但永远不会超过10个成员
  • on request (button press) add an arbitrary number of people to the first group that can take all requested members 根据要求(按下按钮)将任意数量的人添加到第一组,该组可以容纳所有请求的成员
  • if no group can take all requested members, add a new group and add the request to that. 如果没有群组可以容纳所有请求的成员,请添加新群组并将请求添加到该群组。

Assuming this, the easiset way is not to use multi-dimensional arrays ("arrays with two columns): 假设这样做,最简单的方法是不使用多维数组(“两列数组”):

var myArray = [0,0,0];

function groups() {
    var form = document.getElementById('number').value;
    if (form >=1) {  
        for (var i; i<myArray.length; i++) {
            if (myArray[i] < 10 - form) {
                myArray[i] += form;
                return;
            }
        }
        myArray.push(form); // this will only happen if "return" up there never happened
    }
    else {alert("no");
}

If you want that group-name / a multi-dimensional array, you can do it like this: 如果要使用组名/多维数组,可以这样进行:

var myArray = [['Group 0',0], ['Group 1',0], ['Group 2',0]];

function groups() {
    var form = document.getElementById('number').value;
    if (form >=1) {  
        for (var i; i<myArray.length; i++) {
            if (myArray[i][1] < 10 - form) { // myArray[i][1] is the number of reservations in group i, myArray[i][0] would be the group's name
                myArray[i][1] += form;
                return;
            }
        }
        myArray.push(['Group ' + myArray.length, form); // this will only happen if "return" up there never happened
    }
    else {alert("no");
}

For better readability, you can use structured objects inside myArray instead of more arrays: 为了获得更好的可读性,可以在myArray中使用结构化对象,而不是使用更多数组:

var myArray = [{name:'Group 0',reserved:0}, {name:'Group 1',reserved:0}, {name:'Group 2',reserved:0}];

function groups() {
    var form = document.getElementById('number').value;
    if (form >=1) {  
        for (var i; i<myArray.length; i++) {
            if (myArray[i].reserved < 10 - form) { // myArray[i].reserved is the number of reservations in group i, myArray[i].name would be the group's name
                myArray[i].reserved += form;
                return;
            }
        }
        myArray.push({name:'Group ' + myArray.length, reserved:form); // this will only happen if "return" up there never happened
    }
    else {alert("no");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM