mardi 31 mars 2015

nodejs object properities - find empty element

I have an object called obj which has 20 properities. Because I'm building an XML with json xml2js builder, I want to insert only those which are NOT empty. I have the following code (variable result has sql query results from db, and sometimes it happens, that a property in object is undefined, which I want to avoid).



var obj = [] ;
for (var i = 0; i < Object.keys(result[0]).length; i++) {
var value = (result[0])[Object.keys(result[0])[i]];
obj[i] = value;
}


What would be a fastest solution? Using try/catch would make the app slower, or?


Aucun commentaire:

Enregistrer un commentaire