Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
Creating an array of objects with random number of properties

I am trying to create an array of objects that will be populated by random properties. I want the objects all to look like this
{ systemStar: "something random", planets: ["random1", "random2", etc]} Currently I have the following code

const letThereBeLight = () => {
  let universe = []
  const starNumber = 5;
  let randomNumberOfPlanets = Math.floor(Math.random() * 6);
  for (let i = 0; i < starNumber; i++) {
    universe.push({
      systemStar: starList[Math.floor(Math.random() * lengthOfStarList)],
      systemPlanets: [planetList[Math.floor(Math.random() * lengthOfPlanetList)]]
    })

  }

  console.log(universe)
}

This does a great job in creating the objects I want with the following format, however the systemPlanets is only one, instead of a random number. I have tried to do a double for loop but the syntax was eluding me. How can I get an array of random strings inside of my for loop?
Added variables for clarity

let starList = [
  "Red-Giant",
  "Red-Supergiant",
  "Blue-Giant",
  "White-Dwarf",
  "Yellow-Dwarf",
  "Red-Dwarf",
  "Brown-Dwarf",
];
let planetList = [
  "Rocky",
  "Temperate",
  "Ocean",
  "Frozen",
  "Lava",
  "Gas"
];
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

What you need is create another loop of random number that is less than number of available planets, and create an array of planets in that loop:

let starList = [
  "Red-Giant",
  "Red-Supergiant",
  "Blue-Giant",
  "White-Dwarf",
  "Yellow-Dwarf",
  "Red-Dwarf",
  "Brown-Dwarf",
];
let planetList = [
  "Rocky",
  "Temperate",
  "Ocean",
  "Frozen",
  "Lava",
  "Gas"
];

const letThereBeLight = () => {
  let universe = []
  const starNumber = 5;
  let randomNumberOfPlanets = Math.floor(Math.random() * 6);
  for (let i = 0; i < starNumber; i++) {
    const planets = [];
    // generate list of planets with at least 1 planet
    for(let p = 0, max = ~~(Math.random() * planetList.length - 1) + 1; p < max; p++)
      planets[planets.length] = planetList[~~(Math.random() * planetList.length)];

    universe.push({
      systemStar: starList[Math.floor(Math.random() * starList.length)],
      systemPlanets: planets
    })

  }

  console.log(universe)
}
letThereBeLight();

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try using Array.from({length:5}) to create an array of 5 elements. and replace 5 with a random number by using Math.random() * 5 Math.floor() method to round it down to near number, add add one to at least create one planet. then map them to values in source array `planetList.

let starList = [
  "Red-Giant",
  "Red-Supergiant",
  "Blue-Giant",
  "White-Dwarf",
  "Yellow-Dwarf",
  "Red-Dwarf",
  "Brown-Dwarf",
];
let planetList = [
  "Rocky",
  "Temperate",
  "Ocean",
  "Frozen",
  "Lava",
  "Gas"
];
let lengthOfStarList = starList.length;
let lengthOfPlanetList = planetList.length;
let universe = []
const starNumber = 5;
for (let i = 0; i < starNumber; i++) {
    universe.push({
      systemStar: starList[Math.floor(Math.random() * lengthOfStarList)],
      systemPlanets: Array.from({length:Math.floor(Math.random() * 5)+1}).map(x=>planetList[Math.floor(Math.random() * lengthOfPlanetList)])
    })
}
console.log(universe)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda