2

I'm wondering how to get an array of objects with distinct objects. I'm managing a database where we've modifications constantly and we want to get the lasts modifications on objects in order to display them. I'm using NeDB and we can't distinct values so I've got an array of objects which is sorted by date but I don't know how to get an array of objects with unique name.

Distinct objects means :

Imagine I've got 3 objects like

{name: "Toto", modif: "added", createdAt : "05/08/2017"}
{name: "Toto", modif: "updated", createdAt: "05/09/2017"}
{name: "Toto", modif: "deleted", createdAt: "05/10/2017"}

I want to only keep the first Toto here.

We don't care about that date because the array of objects is already sorted by date.

4
  • 2
    distinct object means same object reference or objects with same keys/values? Commented May 9, 2017 at 7:41
  • do you like to keep every first object? Commented May 9, 2017 at 7:51
  • Yep, every first objects according to their names Commented May 9, 2017 at 7:52
  • Yes, my mistake I didn't read myself and wanted to put 3 objects for no misunderstanding Commented May 9, 2017 at 8:06

3 Answers 3

3

You could use a hash table for keeping a flag for same named objects and filter the given array.

var objects = [{ name: "Toto", modif: "added", createdAt: "05/08/2017" }, { name: "Toto", modif: "updated", createdAt: "05/09/2017" }, { name: "Toto", modif: "deleted", createdAt: "05/10/2017" }],
    unique = objects.filter(function (hash) {
        return function (o) {
            if (!hash[o.name]) {
                hash[o.name] = true;
                return true;
            }
        };
    }(Object.create(null)));

console.log(unique);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

3 Comments

return hash[o.name] = true;?
@torazaburo, right, but it's one time bad and one time good to return and make an assignment at the same time. i don't know.
Right up there with the trick of creating magic variables by misusing function parameters.
0

Filter the array to include only those elements such that a search for the first element in the array with that name yields that element itself.

data.filter(elt => data.find(elt2 => elt.name === elt2.name) === elt)

1 Comment

this is O(n^2) though.
0

IMO the most efficient way to solve this is to reduce the array by the key you care about while keeping the first value set for the key.

var uniqueObjectsByKey = function(array, key){
  return Object.values(array.reduce(function(obj, value) { 
    obj[value[key]] = obj[value[key]] !==undefined ? obj[value[key]] : value; 
    return obj 
  }, {}))
}
    
var array = [
  {name: "Toto", modif: "added", createdAt : "05/08/2017"},
  {name: "Toto", modif: "updated", createdAt: "05/09/2017"},
  {name: "Toto", modif: "deleted", createdAt: "05/10/2017"}
]
    
uniqueObjectsByKey(array, 'Toto')

alternatively if you want an ES6 one line where YOUR_ARRAY is your array of objects and FILTER_KEY is the key on which you want unique objects:

Object.values(YOUR_ARRAY.reduce((obj, value) => ({[value[FILTER_KEY]]]: value[FILTER_KEY], ...obj }), {}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.