-
-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathcontains.js
More file actions
19 lines (17 loc) · 806 Bytes
/
contains.js
File metadata and controls
19 lines (17 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function contains(object, property) {
// Check if object is a valid object (not null, array, number, string, etc.)
//if so return false. updated to explicitly check that the input is a plain object, not an array.
if (typeof object !== "object" || object === null || Array.isArray(object)) {
return false;
}
// Check if the object has the specified property as its own property
return object.hasOwnProperty(property);
}
module.exports = contains;
// console.log(contains({ a: 1, b: 2 }, 'a')); // true
// console.log(contains({ a: 1, b: 2 }, 'c')); // false
// console.log(contains({}, 'a')); // false
// console.log(contains([], 'a')); // false
// console.log(contains(1, 'a')); // false
// console.log(contains("NotANumber", 'a')); // false
// console.log(contains(null, 'a')); // false