JavaScript:在 while 循环中更改对象中的值

标签 javascript arrays sorting object javascript-objects

我有一个 JavaScript 对象数组,其中有一个属性“table”,该属性的值可以是 table1、table2、table3 或 table4。属性的值是随机生成的。每张 table 只能有 6 名成员。我有一个 while 循环,它检查“table1”是否有超过 6 个值。我遇到的问题是更改每个“table1”值,直到少于 6 个对象具有“table1”值。

对象数组:

  var data = [{
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
        {
            name: "",
            pref: "",
            table: ""
        },
    ];

While循环:

function countTable(value) {
        return function (sum, item) {
            return sum + (item.table === value);
        };
    }

    function check() {
      while (data.reduce(countTable('table1'), 0) >= 6) {
          // I need help here
      }
    }

感谢您的帮助。

最佳答案

为什么不在随机分配表值时解决问题?通过一点保护,您可以避免分配相同名称超过 6 次:

let counts = [[1, 6], [2, 6], [3, 6], [4, 6]];
data.forEach( obj => {
    let i = Math.floor(Math.random() * counts.length);
    obj.table = 'table' + counts[i][0];
    if(--counts[i][1] == 0) counts.splice(i, 1);
});

const data = [{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
},{
    name: "",
    pref: "",
    table: ""
}];

let counts = [[1, 6], [2, 6], [3, 6], [4, 6]];
data.forEach( obj => {
    let i = Math.floor(Math.random() * counts.length);
    obj.table = 'table' + counts[i][0];
    if(--counts[i][1] == 0) counts.splice(i, 1);
});

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

当您已经随机分配了表值,并且在有超过 6 个相同值的情况下需要重新分配时,它可能如下所示:

let counts = [6, 6, 6, 6];
let tables = [1, 2, 3, 4];
data.forEach( obj => {
    let i = obj.table.slice(-1) - 1;
    if (counts[i]) { // It's OK
        counts[i]--;
        return; 
    }
    let j = Math.floor(Math.random() * tables.length);
    i = tables[j];
    obj.table = 'table' + i;
    counts[i]--;
    if (!counts[i]) tables.splice(j, 1);
});

该代码段以具有太多 table3 值的数据开头:

var data = [{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table1"
},{
    name: "",
    pref: "",
    table: "table2"
},{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table4"
},{
    name: "",
    pref: "",
    table: "table1"
},{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table2"
},{
    name: "",
    pref: "",
    table: "table4"
},{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table4"
},{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table1"
},{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table2"
},{
    name: "",
    pref: "",
    table: "table4"
},{
    name: "",
    pref: "",
    table: "table2"
},{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table4"
},{
    name: "",
    pref: "",
    table: "table3"
},{
    name: "",
    pref: "",
    table: "table1"
},{
    name: "",
    pref: "",
    table: "table2"
},{
    name: "",
    pref: "",
    table: "table3"
}];

let counts = [6, 6, 6, 6];
let tables = [1, 2, 3, 4];
data.forEach( obj => {
    let i = obj.table.slice(-1) - 1;
    if (counts[i]) { // It's OK
        counts[i]--;
        return; 
    }
    let j = Math.floor(Math.random() * tables.length);
    i = tables[j];
    obj.table = 'table' + i;
    counts[i]--;
    if (!counts[i]) tables.splice(j, 1);
});

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

关于JavaScript:在 while 循环中更改对象中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42588657/

相关文章:

c - 需要 C 程序帮助

python - 按日期时间路径组件对 url 列表进行排序

javascript - 如何在 React Native 的数组对象中渲染数组

javascript - 清除 css 和 javascript 浏览器缓存

android - 在android中获取选定月份和年份的日期列表/数组

mysql 序列号按值列(查询 UPDATE)

c# - 如何正确排序此数组?

javascript - 使用 Tagsinput Bootstrap 在 console.log 上重复值

javascript - Mean.js 不工作。无法连接到 MongoDB

C++:使用动态内存分配编写类似于 C realloc() 函数的函数(即更改其大小)