Here are a couple very naive basic solutions. Both of these examples are for demonstration purposes only. Do not use in production code. There are parameter validation and other checks that were left out to simplify the examples. Further, depending on your browser requirements, or available frameworks, there are much faster and better ways to do your equality tests. Think of this as a starting point for more research.
I'll leave it as an exercise to improve these answers.
For your data exclusively, this would work:
var count, isFirst, data = [[1,"07/28/2014"], [2,"07/29/2014"],[3, "07/28/2014"],[1,"07/28/2014"],[4, "07/30/2014"]];
count = 0
/* loop over each element of the array */
for(var x = 0; x < data.length; x++) {
isFirst = true // used to skip the first matching element
/* for each loop iteration, loop over every element in the array */
for(var y = 0; y < data.length; y++) {
/*
check the inner loop element against the outer loop element
to see if they satisfy your equality requirements.
Notice the second set of index operator brackets, this is
how you access the next dimension of the array.
*/
if(data[x][1] === data[y][1]) {
/*
If this is not the first time we've found this match
then this must be a duplicate element, so remove it
*/
if (!isFirst) {
data.splice(y, 1);
count++;
}
isFirst = false // makes sure that future matches are removed
}
}
}
console.log(count);
console.log(data);
For a more general solution one possibility would be to pass in the equality test as an anonymous function:
/* Note, this is the same algorithm as above */
function spliceIf(data, predicate) {
var count = 0, isFirst;
for(var x = 0; x < data.length; x++) {
isFirst = true;
for(var y = 0; y < data.length; y++) {
if (predicate(data[x], data[y])) {
if (!isFirst) {
data.splice(y, 1);
count++;
}
isFirst = false
}
}
}
return count;
}
var items = [[1,"07/28/2014"], [2,"07/29/2014"],[3, "07/28/2014"],[1,"07/28/2014"],[4, "07/30/2014"]];
// Now call the new function and pass in the equality test as the second parameter:
var itemsRemoved = spliceIf(items,
function(a, b) {
/*
This predicate function will be passed to spliceIf. When it
is called from within then spliceIf function, it will be
provided with the inner and outer elements of the loop.
You can then do your equality test however you see fit.
Notice the predicate function must return a value.
This is equivalent to the "data[x][1] === data[y][1]" line
in the example above.
*/
return a[1] === b[1];
}
);
console.log(itemsRemoved);
console.log(items);