Key Value Pairs

1,940 views
Skip to first unread message

Robert B Johnson

unread,
Dec 19, 2021, 1:54:39 PM12/19/21
to Google Apps Script Community
I have a project in which I would Like to grab a range of data from a particular sheet. And Store it in a  Key value pair.

for example, below represents the data in 2 col and 3 rows in my sheet

Joe      23
Frank   45
Sue      25

I would like to pull that data and store it in a JS  dictionary or Key value pairs

 [  [Name: Joe, Age:23] , [Name: Frank, Age:45], [Name: Sue, Age:25] ]

Next I want to Match the Name  from a array full ok the Keys,  and place the value for that key it into the correct position in an data array

Key Array

[ George, Frank, Jim, Sue , Bob, Joe ]

data array

[ 0, 45, 0, 25, 0 23 ]

Any help would be greatly appreciated

Bob J

Tanaike

unread,
Dec 19, 2021, 9:11:09 PM12/19/21
to Google Apps Script Community
About your 1st question, I thought that this post might be useful.
https://tanaikech.github.io/2021/10/24/converting-values-of-google-spreadsheet-to-object-using-google-apps-script/

About your 2nd question, how about the following sample script?

const obj = [{ Name: "Joe", Age: 23 }, { Name: "Frank", Age: 45 }, { Name: "Sue", Age: 25 }];
const search = ["George", "Frank", "Jim", "Sue", "Bob", "Joe"];
const result = search.map(s => {
  const idx = obj.findIndex(({Name}) => Name == s);
  return idx == -1 ? 0 : obj[idx]["Age"];
})
console.log(result)

Reply all
Reply to author
Forward
0 new messages