Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created December 27, 2022 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/d80131e89ed5025bbd40fe19b9e43beb to your computer and use it in GitHub Desktop.
Save tanaikech/d80131e89ed5025bbd40fe19b9e43beb to your computer and use it in GitHub Desktop.
Filtering JSON object using Google Apps Script

Filtering JSON object using Google Apps Script

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 to v % 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.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment