I’ve got this JS Object:
var test = {"code_operateur":[""],"cp_cult":["",""],"annee":["2011"],"ca_cult":[""]}
When I use this function:
for (i in test) {
if ( test[i] == "" || test[i] === null ) {
delete test[i];
}
}
I get:
{"cp_cult":["",""],"annee":["2011"]}
Okay not bad, but I’d like to remove the empty “cp_cult” property (which is an array and not a string like the other).
Note: I don’t want to manually delete the key!
Try:
However, depending on the complexity of the object, you’d need more advanced algorithms. For example, if the array can contain another array of empty strings (or even more levels) and it should be deleted, you’d need to check for that as well.
EDIT: Trying to make something to fit your needs, please have a look at: http://jsfiddle.net/jVHNe/