I have a problem with filtering object's values by true/false, just like in this topic Get object keys for filtered values but without underscore, meaning using exclusively plain JS. There are many more or less similar questions on StackOverflow, but unfortunately I failed to set a working approach.
For example, I have an object:
var anObject = {first_property: false, second_property: true, third_property: false, fourth_property: false, fifth_property: false, sixth_property: true, seventh_property: false, eight_property: nine-nth_property: false}
I need to get a new object, featuring exclusively truthy values, like:
var newObject = { second_property: true, sixth_property: true }
To filter, I wrote the following filter:
function isValid(value) {
if (typeof value === "undefined" || value == null || value.length == 0 || value == false) {
return false;
} else {
return true;
}
}
Broke my head and spent a few hours trying different approach, however the result is unsatisfactory. How should I built the algorithm to do this, which custom/out-of-the-box functions is worth using here? Thanks in advance!
Object, at least name the variableobject;-)