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

134
Vistas
How to create one array from multiple output node.js

I have this code

bot.webex.memberships.list({roomId: bot.room.id})
.then((memberships) => {
  for (const member of memberships.items) {
    if (member.personId === bot.person.id) {
      // Skip myself!
      continue;
    }

    let names = (member.personDisplayName) ? member.personDisplayName :member.personEmail;                                                                 
bot.say(`Hello ${member.personDisplayName`);

Which produce multiple output line by line Like this:

John Dou

Alfred Pennivor

Michel Lee

I need to create one array from this output, randomize this array and bot must say only one random name from array. Please note number of names maybe different

PS I try to use split but get 3 different arrays instead of one.

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

0

First, taking a random element from an array is explained in this question.

For only displaying a random element, which is NOT your own name, then this should work for you. Since memberships.items is already an array then we can directly extract a random element from it.

Example code:

bot.webex.memberships.list({roomId: bot.room.id})
.then((memberships) => {


  let member
  // take random member and repeat until it's not the current user.
  do {
  
    const items = memberships.items

    // get random element
    // https://stackoverflow.com/questions/5915096/get-a-random-item-from-a-javascript-array/5915122#5915122
    member = items[Math.floor(Math.random()*items.length)];

  } while(member.personId === bot.person.id);

bot.say(`Hello ${member.personDisplayName}`)

})

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can filter out the members that aren't you, and then map over that array of object to create a list of names. You then select a random name from that array.

bot
  .webex
  .memberships
  .list({roomId: bot.room.id})
  .then(logRandomName);

Here's a working example based off your code.

const bot = { person: { id: 4, name: 'Joe' } };
const memberships=[{personId:1,name:"Fozzie Bear"},{personId:2,name:"Carlos The Jackal"},{personId:3,name:"Bob From Accounting"},{personId:4,name:"Joe"},{personId:5,name:"Gunther"},{personId:6,name:"Ali"},{personId:7,name:"Renaldo"}];

function logRandomName(memberships) {

  // First `filter` over the memberships array to
  // to produce a new array that doesn't contain the
  // member object that matches the bot.person.id
  const names = memberships.filter(member => {
    return member.personId !== bot.person.id;

  // And then `map` over that array to return an
  // array of names
  }).map(member => member.name);

  const rnd = Math.floor(Math.random() * names.length);

  // Then pick a random name from the names array
  console.log(`Hallo ${names[rnd]}`);

}

logRandomName(memberships);

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