0

I try to draw a graph from tens of thousands points, but because this number is so big i need to reduce it. Many of them have duplicates. I tried to reduce the number using this:

   var array=_.reject(data,function(object,i){
return i>0 && (data[i-1].a === object.a && data[i-1].b===object.b && data[i-1].c===object.c);
});

How can i modify this function, or to create a new one, in order to keep first and last value considered duplicate. Those are different by another attribute 'd' which represent a time stamp.

HelenA
  • 96
  • 1
  • 11

3 Answers3

2
//return filtered points, compareFunction for sorting, equalFunction for
//removing points
function removeDuplicate(data,compareFunction,equalFunction) {
  data.sort(function(pointa, pointb) {
    var compare = compareFunction(pointa,pointb);
    return compare;
  });
  var arr = new Array();
  var prev = new Object();
  var index = 0;
  for (var i = 0; i < data.length; i++) {
    if (i == 0 || !(equalFunction(prev,data[i]))) {
      arr[index++] = data[i];
      prev = data[i];
    }
  }
return arr;
}

function compareFunction(pointa,pointb){
return (pointa.a + pointa.b + pointa.c) - (pointb.a + pointb.b + pointb.c);
}

function equalFunction(pointa,pointb){
return pointa.a == pointb.a && pointa.b == pointb.b && pointa.c == pointb.c;
}

example - https://jsfiddle.net/8xu4Lwp2/

1

The simplest way to eliminate duplicates from an array in JavaScript is to cast it as a Set and then back to an Array. Sets don't store duplicates.

// not sure why setArr isn't logging, it does in Opera console.

arr=[1,1,2,2,3,3];
console.log(arr);
setArr=new Set(arr);
console.log(setArr);
newArr=[...setArr];
console.log(newArr);
JMP
  • 4,417
  • 17
  • 30
  • 41
-1

Cool solution:

var unique = Array.from(new Set(arrayWithDuplicatedValue));
Serginho
  • 7,291
  • 2
  • 27
  • 52
  • Doesn't work with object array `Array.from(new Set([{a:1}, {a:1}]));` – gurvinder372 Dec 07 '17 at 09:27
  • @gurvinder372 That's not an error to vote -1, objects has different references, if you try `{a:1} === {a:1}` the result is `false`. But if you try `var foo = {a:1}; Array.from(new Set([foo, foo]))` then works properly. – Serginho Dec 07 '17 at 11:19
  • Your solution, therefore, is irrelevant to the question at best. OP is comparing objects. – gurvinder372 Dec 07 '17 at 11:20