This is a simple sample script for filtering JSON objects using Google Apps Script.
In the current stage, V8 runtime can be used with Google Apps Script. By this, when you want to filter a JSON object, you can use the following sample script.
Sample script
In this sample script, obj
is filtered by the value of the even number.
const obj = { key1: 1, key2: 2, key3: 3, key4: 4, key5: 5 };
const res = Object.fromEntries(
Object.entries(obj).filter(([, v]) => v % 2 == 0)
);
console.log(res); // {"key2":2,"key4":4}
-
When
v % 2 == 0
is modified tov % 2 == 1
, you can filter the JSON object with the odd number like{"key1":1,"key3":3,"key5":5}
. -
I think that this method might be applied to various situations for managing JSON objects.
-
Of course, this method can also be used for Javascript.