This sample removes duplicate JSON elements for a value of a certain key. When the value of the certain key is removed, only a first duplicate element is left. Also I had wanted to be used for Google Apps Script. So it became like this.
Script :
function removeDup(arr, key){
var temp = [];
var out = [];
arr.forEach( function (e, i) {
temp[i] = (temp.indexOf(e[key]) === -1) ? e[key] : false;
if (temp[i]) out.push(e);
});
return out;
}
JSON :
var arr = [
{a: "a1", b: "b1", c: "c1"},
{a: "a2", b: "b1", c: "c1"},
{a: "a3", b: "b2", c: "c1"},
{a: "a4", b: "b2", c: "c2"},
{a: "a5", b: "b3", c: "c2"}
]
Pattern 1:
var key = "b";
var out = removeDup(arr, "key")
[
{ a: 'a1', b: 'b1', c: 'c1' },
{ a: 'a3', b: 'b2', c: 'c1' },
{ a: 'a5', b: 'b3', c: 'c2' }
]
Pattern 2:
var key = "c";
var out = removeDup(arr, "key")
[
{ a: 'a1', b: 'b1', c: 'c1' },
{ a: 'a4', b: 'b2', c: 'c2' }
]