Working with Arrays of Objects
This post is a corrected post after AI explains the code included in the book “Google Apps Script 101: Building Work Automation For Free”. Added a strikethrough when editing what AI has written, and added color when edited by author
Table of Contents
Summary
In this blog post, we will explore how to work with arrays of objects in Apps Script. We will learn how to access object properties within an array using square brackets and dot notation.
Code
1 2 3 4 5 6 7 8 9 10 11 |
function arrObj(){ let arr = [ {name: 'lee', age: 30}, {name: 'kim', age: 20}, {name: 'park', age: 10} ] Logger.log(arr[0]) Logger.log(arr[0].name) Logger.log(arr[0].age) } |
Code Explanation
The code declares a function named arrObj
. Inside the function, an array arr
is created with three objects, each containing a name
and age
property.
The Logger.log
statements are used to print the first object in the array, as well as its name
and age
properties.
Example
When the arrObj
function is executed, the following values will be logged in the Apps Script Logger:
1 2 3 |
{name: 'lee', age: 30} lee 30 |
AI Prompt
Create a function that initializes an array with three objects, each containing a name
and age
property. Log the first object in the array, as well as its name
and age
properties.