Apps Script Blog – Working with 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 objects in Apps Script. Objects are a fundamental data type in JavaScript and are used to store key-value pairs. We will learn how to define and access object properties, as well as how to create and manipulate objects in our Apps Script projects.
Code
1 2 3 4 5 |
let obj = { key1: 'value1', key2: 'value2', key3: 'value3' } |
Code Explanation
Here, we have created an object named obj
with three key-value pairs. Each key represents a property name, and its corresponding value represents the value of that property. In this example, we have three properties: key1
, key2
, and key3
, with their respective values 'value1'
, 'value2'
, and 'value3'
.
Example
Let’s say we want to access the value of key2
in our obj
object. We can do so by using the dot notation:
1 |
console.log(obj.key2); // Output: 'value2' |
AI generated example successfully. It covers over the code explained here
AI Prompt
Generate the following code using the given prompt:
Write code to create an object with three key-value pairs. Name the object ‘obj’ and assign the keys as ‘key1’, ‘key2’, and ‘key3’, with their respective values as ‘value1’, ‘value2’, and ‘value3’.
1 2 3 4 5 |
let obj = { key1: 'value1', key2: 'value2', key3: 'value3' } |