Javascript looping objects -
i need make function called onlytruthy takes in object, loops through properties, , removes falsy. return object passed in. i'm confused on how make truthy or falsey. have far.
function onlytruthy (val) { (var key in val) { if (val.hasownproperty(key)) { alert(key + " -> " + val[key]); } } return val; }
i think better create new variable hold true values of existing object shown below:
var youroldobject; /* data in */ var newobject = onlytruthy(youroldobject); /* newobject variable holds true values */ function onlytruthy (val) { var newobj = {}; (var key in val) { if (val.hasownproperty(key) && val[key]) { newobj[key] = val[key]; } } return newobj; }
Comments
Post a Comment