Создание массива объектов с циклом for в JavaScript - PullRequest
0 голосов
/ 25 августа 2018

Я хочу создать массив объектов с циклом 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?

Ответы [ 2 ]

0 голосов
/ 25 августа 2018

Разрешение - Заменить 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());
0 голосов
/ 25 августа 2018
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 объектами.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...