Skip to main content
Question

Remove duplicates from Concatenated Child to Parent Field - Involves Javascript

  • October 6, 2017
  • 0 replies
  • 75 views

I have a field that is using Dan Diebolt's IOL technique that is Concatenating child fields to the parent record into one combined field.  I would like for this field to remove all duplicates and blanks. Can someone please help?  Field 173 that is being combined might say the same thing in each record so it is repeating in my combined field and I want it to only be listed once.  Field 173 may also be blank and I do not want the blank spaces in the combined field.  I appreciate any help!  Thank you!

Here is the code I am using: 

(function(){
  var XMLFlatToObj = function(xml, type) {
    var data = [];
    var record = {};
    $("record", xml).each(function() {
      record = {};
      $(this).children().each(function () {
        record[$(this).prop("tagName")] = $(this).text();
      });
      data.push(record);
    });
    return {records: data};
  }
  var dbid = "bjvehdwbq";
  var dbidParents = "bjvehdwcb";
  var dbidChilds = "bjvehdwcj";
  var apptoken = "d5ju6zccf9kf3bdbi87gsj8yz8m";
  $.ajaxSetup({data: {apptoken: apptoken}});
  var rids = $("span.QBU_Childs").map(function() {
    return this.dataset.rid;
  }).get();
  var n= 100;
  var ridsChunked = _.chain(rids).groupBy(function(element, index){
    return Math.floor(index/n);
  }).toArray().value();
  _.each(ridsChunked, function(rids, index) {
    var query = "{22.EX." + rids.join("}OR{22.EX.") + "}"
    $.ajax({
      url: dbidChilds,
      data: {
        act: "API_DoQuery",
        clist: "3.22.173",
        query: query
      },
      dataFilter: XMLFlatToObj
    }).then(function(resp) {
      var childsData = _.groupBy(resp.records, function(items) {
        return items["related_lot"];
      });
      _.each(childsData, function(item, rid) {
        $("span.QBU_Childs[data-rid=" + rid + "]").html( _.pluck(item, "combined_defects").join("<span style='color:red;'> | </span>") );
      });
       
    });
  }); 
})();
This topic has been closed for replies.

  • Author
  • Quickbase Alumni
  • October 6, 2017
Oh and I'd also like the result to be sorted A-Z

  • Quickbase Alumni
  • October 6, 2017
I see underscore in there so you can use that by using this. Just pass your array of words through this before sending to the concat field. I haven't used underscore a lot so Dan may chime in with a way better answer as always :P
http://underscorejs.org/#uniq

_.uniq(data);


Another examle
_.uniq([1, 2, 1, 4, 1, 3]); 
=> [1, 2, 4, 3]
For sorting you can then pass it through something like this _.iteratee(value, [context])  

  • Quickbase Alumni
  • October 9, 2017
Replace this block of code:
 _.each(childsData, function(item, rid) {
   $("span.QBU_Childs[data-rid=" + rid + "]").html( _.pluck(item, "combined_defects").join("<span style='color:red;'> | </span>") );
 });
with this block of code:
 _.each(childsData, function(item, rid) {
   var markup = _.chain(item)
     .pluck("combined_defects")
     .uniq()
     .value()
     .join("<span style='color:red;'> | </span>")
   $("span.QBU_Childs[data-rid=" + rid + "]").html(markup);
 });