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'