Given your origin array:
var array = [
{name:"Joe", date:'2018-07-01', amt:250 },
{name:"Mars", date:'2018-07-01', amt:250 },
{name:"Joe", date:'2018-07-02', amt:250 },
{name:"Saturn", date:'2018-07-01', amt:250 },
{name:"Joe", date:'2018-07-02', amt:250 },
{name:"Jupiter", date:'2018-07-01', amt:250 }
];
You need a function to determine the criteria you want to check to make your object comparable with anothers.
UPDATE
Such a function could be an hash function that you should write to create a unique key to identify your objects and compare each other.
The proper hash function should return the same value if called with objects that have the same composed key.
In your case, when 2 objects have name, date and amount with the same value, the hash function is supposed to return the same value.
So you can directly compare hash values instead of objects.
In the case you asked for, all the values concurred to the filter, and here is a very trivial example of such a function, that is not proper an "hash" as the output is variable (should produce a fixed length value instead):
function objectHash (obj) {
return Object.values(obj).reduce((a, b) => {
a += b;
return a;
}, '');
}
Now, you are now able to easily and properly compare items in your array in order to filter duplication.
var arrayMap = array.reduce((acc, item) => {
var hash = objectHash(item);
if (typeof acc[hash] === 'undefined') {
acc[hash] = Object.assign({}, item);
}
return acc;
}, {})
Then you have an object with unique key values, and to have back your unique array:
var uniqueArray = Object.values(arrayMap);
Now you have your data.
name
value but differentdate
andamt
. do you need to exclude them or not?array = _.uniqBy(array, 'name'); array = _.uniqBy(array, 'date'); array = _.uniqBy(array, 'amt');
But it's not efficient_.uniq(array);
, only you need lodash libraryindexOf
function when passed an object will only compare the object's reference for an exact match within the array, and does not do a deep comparison on the keys or values therein.