2

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'
hgb123
  • 13,869
  • 3
  • 20
  • 38
Madush Nim
  • 41
  • 4
  • So where's your attempt at implementing the functionality? Where are you stuck? – Bergi Nov 21 '22 at 03:18
  • I try to use "props instanceof Array===true" to check that properties are an instance of a array or not but that part not working. – Madush Nim Nov 21 '22 at 03:31
  • you need to use Array.isArray(x) – Andrew Parks Nov 21 '22 at 03:35
  • What output are you expecting when there are objects that match that argument? An array of objects, or an array of `name` values eg `['Jony', 'Kane']`? – Andy Nov 21 '22 at 03:54
  • 1
    Why do you expect the rest parameter to be anything else than an array? – gre_gor Nov 21 '22 at 03:55
  • 1
    Does this answer your question? [Create array of unique objects by property](https://stackoverflow.com/questions/18773778/create-array-of-unique-objects-by-property) – gre_gor Nov 21 '22 at 03:59
  • @Andy When I pass 'name' as a parameter I want to get unique data of the array object according to the 'name' parameter.( [ {id: 1, name: "Jony"}, {id: 3, name: "Kane"}]). That result is okay, my question is about the third 'console.log' output, I want to return 'Invalid Parameter Type' when we pass invalid parameters. – Madush Nim Nov 21 '22 at 04:01
  • @MadushNim what is a valid parmeter in your case? Let's say if you want any array to be the input, things can be as simple as: ```if (Array.isArray(input)) 'do something' else return 'Invalid Parameter Type' ``` – Ping Nov 21 '22 at 04:55

1 Answers1

0

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'));
Andy
  • 61,948
  • 13
  • 68
  • 95