在JavaScript中使用for循环创建对象数组

时间:2018-08-25 01:09:34

标签: javascript arrays object for-loop

我想使用另一个数组中的值通过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

  

如何生成与{{1}}数组中一样多的值?

2 个答案:

答案 0 :(得分:2)

  

解决方案-将i < names.length - 1替换为i < 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());

答案 1 :(得分:1)

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 < names.length;,而不是i < 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个对象的数组。