Home > Enterprise >  How to unique array object values?
How to unique array object values?

Time:11-21

Below you can see my NodeJS code and here I want to print values of the object that unique with the name.

Array.prototype.uniqueArray = function (...props) {

  if (!props || (props instanceof Array && props.length === 0)) {
    return [...this]
  } else if (props instanceof Array===true) {
    return 'Invalid Parameter Type'
  }else{
    console.log("test 123")
  }
}

console.log([ ].uniqueArray('name') )
console.log([ {id: 1, name: "Jony"}, {id: 2, name: "Jony"}, {id: 3, name: "Kane"}].uniqueArray('name') )
console.log([ {id: 1, name: "Jony"}, {id: 2, name: "Jony"}, {id: 3, name: "Kane"}].uniqueArray('test') )

I want to print, if we input empty array that return same empty array (like []), And if we pass parameter like 'name', it will return the unique data according to the 'name'. So I can get those outputs correctly.

But as the third 'console.log' part, I want to return "Invalid Parameter Type" when we pass invalid parameter that not include in the array object like 'test'. For third 'console.log' part that I can get below result:

Output :

[ { id: 1, name: 'Jony'} ] 

Expected Output :

'Invalid Parameter Type'

CodePudding user response:

One approach might be to dedupe the objects into a new array. You'll know if the argument wasn't found in any of the objects if that array is empty.

// Double check that uniqueArray doesn't exist
// on the array prototype
if (!('uniqueArray' in Array.prototype)) {
  
  // You're only sending in one argument to the
  // function so there's no need to spread it.
  Array.prototype.uniqueArray = function(prop) {
    
    // If there is no prop or or there is but it's not
    // a string, or the array is empty, return the array
    if ((!prop || typeof prop !== 'string') || !this.length) {
      return this;
    }

    // Iterate over the array of objects with reduce,
    // initialising it with a Map. This will hold our deduped
    // objects.
    const deduped = this.reduce((acc, obj) => {
      
      // Using the prop argument find the key which is
      // the value of that property in the object.
      const key = obj[prop];
      
      // If that key doesn't exist on the Map add it,
      // and set its value as the object
      if (key && !acc.has(key)) acc.set(key, obj);
      
      // Return the Map for the next iteration
      return acc;

    }, new Map());
    
    // Grab the values from the Map, and coerce them back
    // to an array
    const arr = [...deduped.values()];

    // If there are no objects in the array it means there were
    // no objects with values that matched the argument, so
    // return a message
    if (!arr.length) return 'Invalid Parameter Type';
    
    // Otherwise return the array
    return arr;
  
  }

}

console.log([ ].uniqueArray('name'));
console.log([ ].uniqueArray(23));
console.log([ {id: 1, name: "Jony"}, {id: 2, name: "Jony"}, {id: 3, name: "Kane"}].uniqueArray('name'));
console.log([ {id: 1, name: "Jony"}, {id: 2, name: "Jony"}, {id: 3, name: "Kane"}].uniqueArray('test'));

  • Related