Ace
Ace

Reputation: 55

Creating an array of objects with for loop in JavaScript

I want to create an array of objects with for loop using values in another array.

Code Snippet below generates 5 values, instead of 6 (as required)

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());

How do I generate as many values as present in names array ?

Upvotes: 0

Views: 725

Answers (2)

nkshio
nkshio

Reputation: 1080

Resolution - Replace i < names.length - 1 with i < names.length

The condition for executing the code block in for loop is wrong. Your code works fine, just generates 1 less result than needed.

MDN web docs on how for works.

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());

Upvotes: 2

H. Majury
H. Majury

Reputation: 376

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)
    }
];

Inside the for loop: i < names.length;, instead of 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;
}

This returns an array with all 6 objects.

Upvotes: 1

Related Questions