javascript retrieve value from a map

Viewed 52

I am trying to develop a google script app.

Here is one function to create an array map.

function getOffices(){
 var result=AdminDirectory.Groups.list({domain:"example.com"})
  result=result.groups.filter(function(group){
    var str=group.email;
     return str.search("-office@example.com")>=0;
  })
  result=result.map(function(group){ return {name:group.name,email:group.email}})
 
  return result;
}

I have created a logic piece, that I want to execute certain actions based on the results, that looks like this:

var getOrgUnitPath = (accountOffice, accountType) => {
if (accountType === 'facilitator') {
  return 'Limited Accounts/Gmail Plus Calendar';
} else {
  switch (accountOffice) {
    case accountOffice.includes('Boston'):
      return "/Standard-Access/Boston";
      break;
    case accountOffice.includes('New York'):
      return '/Standard-Access/New York';
      break;
    case accountOffice.includes('Lincoln'):
      return '/Standard-Access/Lincoln';
      break;
    default:
      return '/Standard-Access';
      break;
  }
}

};

Lastly, I try to set the organizational unit -- which is ultimately what i am trying to do, but can't seem to get the syntax right, I have tried everything I can think of. I have hardcoded the "accountType" and it worked, so I know the formObject.accountType is functioning properly.

orgUnitPath: getOrgUnitPath(accountType, formObject.accountType),

Thanks in advance!

2 Answers

This is a wrong usage of switch case.

if accountOffice's would be just New York, Boston, Lincoln. Remove the complex condition and replace with

switch (accountOffice) {
  case "Boston":
    return "/Standard-Access/Boston";
    break;
  case "New York":
    return "/Standard-Access/New York";
    break;
  case "Lincoln":
    return "/Standard-Access/Lincoln";
    break;
  default:
    return "/Standard-Access";
    break;
}

If not, use if-else if you have complex condition to check rather than simple match cases

if (accountOffice.includes("Boston")) {
  return "/Standard-Access/Boston";
} else if (accountOffice.includes("New York")) {
  return "/Standard-Access/New York";
} else if (accountOffice.includes("Lincoln")) {
  return "/Standard-Access/Lincoln";
} else {
  return "/Standard-Access";
}

I rewrote the code so I could get a better understanding of it. From what I can tell, getOffices lists all offices and getOrgUnitPath returns a path including the first office that matches the ordered list of offices ['Boston', 'NY', 'Lincoln']. If that's the case, what's missing is that the first argument to getOrgUnitPath should be getOffices(), right? (Notice it is the execution of the function getOffices.)

Here's the code "simplified" to my liking. I hope it helps:

const getOffices = () => {
  const bigList = x.y.list({ domain: 'example.com' }) // ?
  return bigList
    .filter(cur => ~cur.email.search('abc'))
    .map(cur => ({
      name: cur.name,
      email: cur.email
    }))
}

const getPath = (accOffice, accType) => {
  if (accType === 'xyz')
    return 'foobar'
  const city = ['Boston', 'NY', 'Lincoln']
    .find(cur => accOffice.includes(cur))
  return `yadayada/${city}`
}

const theFinalObj = {
  orgUnitPath: getPath(getOffices(), 'rightHardcodedType')
}
Related