1

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.

2 Answers 2

1

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}`)

})

Sign up to request clarification or add additional context in comments.

1 Comment

Hello @Silvan Bregy! Thanks! It's works!)
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.