Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda