how can i check if any javaScript object's property exists and if it exists then it has a valid value? actually,i am a beginner and trying to solve this-
Check if the second argument is truthy on all objects of the first argument(which is an array of objects).i.e.
- check if the second argument exists in all the objects in first argument(an array) as a property.
if it exists, it should not be- - invalid, as age can't be 0
- null
- undefined
- empty string('')
- NaN
till now i have this-
function truthCheck(collection, pre) {
for(var i=0;i<collection.length;i++){
if(!(pre in collection[i])||collection[i]
[pre]===undefined||isNaN(collection[i]
[pre])||collection[i][pre]===""||
collection[i][pre]===null||collection[i]
[pre]===0)
{
return false;
}
}
return true;
}
i know this is not the best wayto solve .Is there a better way to do this?i don't like that long if statement in my code.i have seen other SO links-link1,link2 but none of them seemed to solve my query. any kind of help is highly appreciated. P.S. this code is not working for some true cases even.