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

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -