Reputation: 4262
I want to check if all properties of an object are undefined I tried this but this is always true because when one of them is undefined it is true:
// Check if property is undefined
for (var property in filters) {
if (Object.keys(property).indexOf(undefined)) {
return this.restaurantsList;
}
}
The filters object looks like this:
{
ishnationality: undefined
dishtype: undefined
organizeby: undefined
}
How can I simply check if all are undefined?
Upvotes: 7
Views: 9638
Reputation: 1015
Here is an example evaluating the value of each property in the object:
function allUndefined(obj){
for(var p in obj) {
if(obj[p] !== undefined){
return false;
}
}
return true;
}
var filtersAllUndefined = {
ishnationality: undefined,
x: undefined
}
console.log(allUndefined(filtersAllUndefined));
var filterSomeUndefined = {
ishnationality: undefined,
x: 3
}
console.log(allUndefined(filterSomeUndefined));
Upvotes: -1
Reputation: 1042
function checkForUndefined(object) {
for (var key in object) {
if (object[key] !== undefined)
return true;
}
return false;
}
How about this function?
Upvotes: 0
Reputation: 138235
Object.values(filters).every(el => el === undefined)
You are actually looking for the objects values, not its keys.
Upvotes: 26