Home > OS >  Getting the value of key/value pair in Google App Script
Getting the value of key/value pair in Google App Script

Time:11-16

I have two files in an App Script project:

#Code.gs
function myFunction() {
  ksu = "Kansas State University";
  Logger.log(lgu_map[ksu]);
}

#data.gs
function lgu_map() {
  map["University of Arkansas Pine Bluff"] = "1890";
  map["University of Maryland Eastern Shore"] = "1890";
  map["Virginia State University"] = "1890";
  map["West Virginia State University"] = "1890";
  map["Iowa State University"] = "North Central";
  map["Kansas State University"] = "North Central";
  map["Michigan State University"] = "North Central";
  map["North Dakota State University"] = "North Central";
  map["Ohio State University    North"] = "Central";
}

What I am wanting to do is query the lgu_map() function in data.gs to return is value. So in this case, what I'm wanting Logger.log(lgu_map[ksu]); to return is North Central.

Not even sure if I can call the function with way without actually passing an argument.

Any help appreciated

CodePudding user response:

Try it this way:

const myMap = new Map();
myMap.set("University of Arkansas Pine Bluff", "1890");
myMap.set("University of Maryland Eastern Shore", "1890");
myMap.set("Virginia State University", "1890");
myMap.set("West Virginia State University", "1890");
myMap.set("Iowa State University", "North Central");
myMap.set("Kansas State University", "North Central");
myMap.set("Michigan State University", "North Central");
myMap.set("North Dakota State University", "North Central");
myMap.set("Ohio State University    North", "Central");
myMap.get("University of Arkansas Pine Bluff");//1890

CodePudding user response:

Thanks copper, the syntax I was using to create a map seemed to be the issue. I still ended up having to pass the key to the function; not sure there is a way around that. Here is the complete working code for what I was wanting to do:

#code.gs
function myFunction() {
  ksu = "Kansas State University";
  Logger.log(lgu_map(ksu));
}

#data.gs
function lgu_map(key) {
  const myMap = new Map();
  myMap.set("University of Arkansas Pine Bluff", "1890");
  myMap.set("University of Maryland Eastern Shore", "1890");
  myMap.set("Virginia State University", "1890");
  myMap.set("West Virginia State University", "1890");
  myMap.set("Iowa State University", "North Central");
  myMap.set("Kansas State University", "North Central");
  myMap.set("Michigan State University", "North Central");
  myMap.set("North Dakota State University", "North Central");
  myMap.set("Ohio State University    North", "Central");
  return myMap.get(key);

}

  • Related