Reputation:
If we have:
var obj = {
key: [1000, 10, 50, 10]
};
-If the array at the given key is empty, it should return 0.
-If the property at the given key is not an array, it should return 0.
-If there is no property at the given key, it should return 0.
I'm trying to get the average of the elements at the property (key) with a function, getAverageOfElementsAtProperty(obj, 'key').. I managed that part with exception of the 3 points above.
I tried this:
if (obj[key].constructor != Array || !obj.hasOwnProperty(key) ||
obj[key] == []) {
return 0;
}
But I'm unsure if using three or operational is the correct move...
Upvotes: 0
Views: 1936
Reputation: 9878
So for each condition you mentioned it can be done as follows
If the array at the given key is empty, it should return 0.
obj.key.length === 0
If the property at the given key is not an array, it should return 0.
!Array.isArray(obj[key])
If there is no property at the given key, it should return 0.
!obj.hasOwnProperty("key")
.
You can directly check for a falsy
value to check the existence. Also, check if the the value is an array by using the Array.isArray
function and then for checking the length of array, use the length
property.
if(!obj.hasOwnProperty("key") || !Array.isArray(obj[key]) || obj.key.length === 0)){
return 0;
}
Upvotes: 1
Reputation: 31692
if (obj[key].constructor != Array || ...
will throw an error if obj[key]
is undefined
because, then, you'll be accessing constructor
of undefined
which will throw an error. What you need to do is check if a the value obj[key]
exist then if it is an array like this:
if(obj[key] !== undefined || obj[key].constructor != Array)
return 0;
In general: The or
(||
) operator will stop checking (evaluating) after the first valid check. Consider check0 || check1 || ... || checkN
, if checki
is true
then checki+1 ... checkN
won't be evaluated at all. Because only one valid check is sufficient. Here is an example:
var n = 5;
if(n == 5 || console.log("Here 1"))
;
n = 4;
if(n == 5 || console.log("Here 2"))
;
As you can see, Here 1
is never loged because the code console.log("Here 1")
is never reached.
Upvotes: 0
Reputation: 7258
You can check, first, [1] if the key exists, then [2] if it's array, and, then, [3] if it's not empty.
Order matters: due to Short-circuit evaluation
, if it evaluates true in the first condition, it skips nexts checks (then, you can safely assume that the key exists, that it's an array and so on...)
var obj = {
key: [1000, 10, 50, 10],
key2: [],
key3: "abc"
};
function isValid(obj, key) {
if (
(typeof obj[key] === 'undefined') || //If there is no property at the given key (first)
(!Array.isArray(obj[key])) || //If the property at the given key is not an array (IE9>=)
(obj[key].length == 0) //If the array at the given key is empty,
) {
return 0;
} else {
return 1; /* or whathever you want */
}
}
console.log(isValid(obj, "key")); //1
console.log(isValid(obj, "key2")); //0
console.log(isValid(obj, "key3")); //0
console.log(isValid(obj, "key4")); //0
(typeof obj[key] === 'undefined')
is not necessary here, because Array.isArray(undefined) === false
(check docs), so, you can skip this check.
Upvotes: 0
Reputation: 5472
I like to break up my if
into the business logic that it needs to fulfill. I find it easier to understand when I get back to it later on. So I would do it this way
if (!obj.hasOwnProperty(key)) return 0; // If there is no property at the given key
if (!Array.isArray(obj[key])) return 0; // If the property at the given key is not an array
if (obj[key].length === 0) return 0; // If the array at the given key is empty
// add this point we know there is a valid array
Upvotes: 0
Reputation: 318182
You can check multiple conditions like this
if (
typeof obj === 'object' && // you have an object
'key' in object && // it contains a "key"
Array.isArray( obj['key'] ) // it is an array
)
Upvotes: 2