Addition of Object.hasOwnProperties() method for Object Prototype

I have this method:

function hasOwnProperties(object, array) {
if (object) {
if (typeof object === 'object') {
if (object !== null) {
if (array) {
if (Array.isArray(array) === true) {
if (array.length > 1) {
for (let i = 0; i < array.length; i++) {
if (object.hasOwnProperty(array[i]) === true) {
continue;
} else {
return false;
}
}
return true;
} else {
throw new Error('Parameter two has length of zero.');
}
} else {
throw new Error('Parameter two is not of type Array.');
}
} else {
throw new Error('Parameter two does not exist.');
}
} else {
throw new Error('Parameter one is null');
}
} else {
throw new Error('Parameter one is not of type Object');
}
} else {
throw new Error('Parameter one does not exist.');
}
}

Having engaged in node and post requests I have found it necessary to help speed things along a modified version of that method that instead of having it go through one property it would go through a series of them and be called object.hasOwnProperties with a boolean as its return value. This is would allow for a way to verify as needed in certain cases an extended list of properties as they match the object that is requiring them.

Example: in a post request I have a certain amount of parameters being passed that need to match of with a predefined object's property requirements. This method would allow for those sent parameters to be compared against that model and if verified can be used as a condition to continue further along with whatever parsing or computations need to occur.

There is also a larger suggestion overall for this when dealing with iterative functions and functionality, and that is default functions that deal with singular values and sibling methods that deal with multiple values if at all possible being developed alongside each other as suggested above.

Would some form of this:


(obj, array) => {

const keys = Object.keys(obj);

return array.length > 0 && new Set(keys.concat(array)).size === keys.size;

}

give you the semantic you want?

Yeah, it would. Do you think an actual standard implementation of this would be useful? I think of it as being similar to the every and some functions, personally.

I’d think it would require a lot more motivating use cases, as well as some examples of widely used userland abstractions, to make a compelling case that it belongs in the standard library.

It’s also worth noting that if the Set methods proposal advances, the body of this function would become new Set(array).isSubsetOf(Object.keys(obj))

1 Like