【问题标题】:Creating an array of objects with for loop in JavaScript在 JavaScript 中使用 for 循环创建对象数组
【发布时间】:2018-08-25 01:09:33
【问题描述】:

我想使用另一个数组中的值创建一个带有for 循环的对象数组。

下面的代码片段生成 5 值,而不是 6(根据需要)

function generateArray() {
    var names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
    var newObj = [];

    for (i = 0; i < names.length - 1; i++) {
        newObj[i] = {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }
    }

    return newObj;
}

console.log(generateArray());

如何生成与 names 数组中存在的值一样多的值?

【问题讨论】:

  • 似乎正确地为我生成了 5 个随机对象。如果你想要 6 个对象,只需将迭代次数增加一即可。
  • 你测试的是哪一个? php 代码可以工作,但 javascript 函数代码不行。
  • 只需按“运行代码 sn-p”。您的代码已经有效。
  • 等等,按运行代码sn-p是怎么回事。我尝试在 chrome 中执行它没有工作...
  • 是的,我的代码确实有效,谢谢大家。

标签: javascript arrays object for-loop


【解决方案1】:

解决方案 - 将 i &lt; names.length - 1 替换为 i &lt; names.length

for循环中代码块的执行条件错误。 您的代码运行良好,只是产生的结果比需要的少 1 个。

MDN web docs 了解for 的工作原理。

function generateArray() {
    names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
    newObj = [];

    for (i = 0; i < names.length; i++) {
        newObj[i] = {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }
    }

    return newObj;
}

console.log(generateArray());

【讨论】:

    【解决方案2】:
    names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
    
    arob = [
        {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }, 
        {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }, 
        {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }, 
        {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }, 
        {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }, 
        {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }
    ];
    

    在 for 循环内:i &lt; names.length;,而不是 i &lt; names.length - 1;

    function generateArray() {
        names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
        newObj = [];
    
        for(i=0; i < names.length; i++) {
        newObj[i] = {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20) }
        }
    
        return newObj;
    }
    

    这将返回一个包含所有 6 个对象的数组。

    【讨论】:

      猜你喜欢
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-11
      • 1970-01-01
      • 2012-12-27
      • 2020-09-28
      • 1970-01-01
      相关资源
      最近更新 更多