Duplicate

Retrieving Values with and without Duplicating from JSON Object using Google Apps Script

Gists

This is a sample script for retrieving the values with and without duplicating from JSON object using Google Apps Script. Also this can be used by Javascript.

Sample script

var obj = [
  { key1: "value1a", key2: "value1b" },
  { key1: "value2a", key2: "value2b" },
  { key1: "value5a", key2: "value5b" },
  { key1: "value3a", key2: "value3b" },
  { key1: "value1a", key2: "value1b" },
  { key1: "value4a", key2: "value4b" },
  { key1: "value5a", key2: "value5b" },
  { key1: "value3a", key2: "value3b" }
];

var res = obj.reduce(
  function(obj, e) {
    if (
      obj.withoutDuplicating.some(function(f) {
        return f.key1 === e.key1 && f.key2 === e.key2;
      })
    ) {
      obj.withDuplicating.push(e);
    } else {
      obj.withoutDuplicating.push(e);
    }
    return obj;
  },
  { withoutDuplicating: [], withDuplicating: [] }
);

Logger.log(res);

Result

{
  "withoutDuplicating": [
    {
      "key1": "value1a",
      "key2": "value1b"
    },
    {
      "key1": "value2a",
      "key2": "value2b"
    },
    {
      "key1": "value5a",
      "key2": "value5b"
    },
    {
      "key1": "value3a",
      "key2": "value3b"
    },
    {
      "key1": "value4a",
      "key2": "value4b"
    }
  ],
  "withDuplicating": [
    {
      "key1": "value1a",
      "key2": "value1b"
    },
    {
      "key1": "value5a",
      "key2": "value5b"
    },
    {
      "key1": "value3a",
      "key2": "value3b"
    }
  ]
}

Note

  • As other situation, when f.key1 === e.key1 && f.key2 === e.key2 is modified to f.key1 === e.key1, the duplication of key1 can be retrieved.

Reference

Processing Duplicated Rows of 2 Dimensional Arrays using Google Apps Script

Gists

Overview

These are sample scripts for processing the duplicated rows of 2 dimensional arrays using Google Apps Script.

Description

When I use Google Spreadsheet and/or see Stackoverflow, I sometimes see the situation which is required to process the duplicated rows of 2 dimensional arrays. I thought that when the sample scripts for it have already prepared, they will be useful for other users including me. So I published this post. This sample scripts can be also used for Javascript. If this post is useful for you, I’m glad.

Changing Values by Checking Duplicated Values of JSON for Javascript

Gists

This sample script is for changing values by checking duplicated values of JSON for Javascript.

Please see the following script. There is an array with a JSON data with 3 keys and 3 values. It is found that the values for each element duplicate. These duplicated values are changing by adding numbers.

I use this for managing filenames. This script also can be used for Google Apps Script. If this was useful for you, I’m glad.