Removal of duplicate objects from JSON array

Deepak Singh Ra
Tera Contributor

Hi Team

 I have an array that looks like this:

var standardsList = [
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Geometry"},
    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},
    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},
    {"Grade": "Math 1", "Domain": "Orders of Operation"},
    {"Grade": "Math 2", "Domain": "Geometry"},
    {"Grade": "Math 2", "Domain": "Geometry"}
];

And I need to remove the duplicates so that something like this remains:

var standardsList = [
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Geometry"},
    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},
    {"Grade": "Math 1", "Domain": "Orders of Operation"},
    {"Grade": "Math 2", "Domain": "Geometry"}
];


Is there any way to achieve this?

Thanks
3 REPLIES 3

Willem
Giga Sage
Giga Sage

You can use this:

(Run dedupe function with the array you want)

var list = [
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Geometry"},
    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},
    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},
    {"Grade": "Math 1", "Domain": "Orders of Operation"},
    {"Grade": "Math 2", "Domain": "Geometry"},
    {"Grade": "Math 2", "Domain": "Geometry"}
];

function dedupe(arr) {
  return arr.reduce(function(p, c) {

    // create an identifying id from the object values
    var id = [c.Grade, c.Domain].join('|');

    // if the id is not found in the temp array
    // add the object to the output array
    // and add the key to the temp array
    if (p.temp.indexOf(id) === -1) {
      p.out.push(c);
      p.temp.push(id);
    }
    return p;

    // return the deduped array
  }, {
    temp: [],
    out: []
  }).out;
}

gs.print(JSON.stringify(dedupe(list)));

 

Result:

find_real_file.png

[{"Grade":"Math K","Domain":"Counting & Cardinality"},
{"Grade":"Math K","Domain":"Geometry"},
{"Grade":"Math 1","Domain":"Counting & Cardinality"},
{"Grade":"Math 1","Domain":"Orders of Operation"},
{"Grade":"Math 2","Domain":"Geometry"}]

 

Based on:

https://stackoverflow.com/questions/36032179/remove-duplicates-in-an-object-array-javascript

Willem
Giga Sage
Giga Sage

Using standardsList and setting that value:

var standardsList = [
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Counting & Cardinality"},
    {"Grade": "Math K", "Domain": "Geometry"},
    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},
    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},
    {"Grade": "Math 1", "Domain": "Orders of Operation"},
    {"Grade": "Math 2", "Domain": "Geometry"},
    {"Grade": "Math 2", "Domain": "Geometry"}
];

function dedupe(arr) {
  return arr.reduce(function(p, c) {

    // create an identifying id from the object values
    var id = [c.Grade, c.Domain].join('|');

    // if the id is not found in the temp array
    // add the object to the output array
    // and add the key to the temp array
    if (p.temp.indexOf(id) === -1) {
      p.out.push(c);
      p.temp.push(id);
    }
    return p;

    // return the deduped array
  }, {
    temp: [],
    out: []
  }).out;
}

standardsList = dedupe(standardsList);

How can this be done in a service portal widget?

var arr = [];
var gr = new GlideRecord('u_table');
gr.addEncodedQuery('u_name=' + usr_sysid + '^u_active=true');
gr.query();
while(gr.next()) {
	var obj = {};
	obj.name = gr.getDisplayValue('u_name');
	obj.nameSysID = gr.getValue('u_name');
	obj.cat = gr.getDisplayValue('u_category');
	obj.catSysID = gr.getValue('u_category');
	obj.group = gr.getDisplayValue('u_group');
	obj.groupSysID = gr.getValue('u_group');
	data.arr.push(obj);
}

This is the server-side code that I have in the widget. Before pushing object to the array, want to check if the array has any duplicate values.

Thanks!