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

155
Vistas
How I am supposed to get a random string output from an object in JS?

I need help with the following problem: How is it possible to extract a random value from the object below?

const hellos = {
English: "Hello",
Japanese: "Konnichiwa",
German: "Hallo",
Spanish: "Hola",
Arabic: "Ahlan wa sahlan",
Chinese: "Nihao",
};

random output in console to test. E.g. "konnichiawa", "hola", etc.

I couldn't write my code down, because it's still hard for me as a beginner in JS. Please kindly give me some tips. Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Checkout https://stackoverflow.com/a/15106541/13583510 which picks a random key and return the value.
Since the key doesn't seem to be important as you haven't mentioned in your case you can choose a random value from the values array of the Object (Object.values(hellos)).
The answer in the link gives access to both the key and value so you can use it as well since retrieving a value from a given random key can be achieved in constant time.

const hellos = {English: "Hello",Japanese: "Konnichiwa",German: "Hallo",Spanish: "Hola",Arabic: "Ahlan wa sahlan",Chinese: "Nihao",};

var randomVal = function (obj) {
    var vals = Object.values(obj);
    return vals[ vals.length * Math.random() << 0];
};

console.log(randomVal(hellos))

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