Javascript - removing undefined fields from an object [duplicate]
问题 This question already has answers here : Remove blank attributes from an Object in Javascript (31 answers) Closed 2 years ago . Is there a clean way to remove undefined fields from an object? i.e. > var obj = { a: 1, b: undefined, c: 3 } > removeUndefined(obj) { a: 1, c: 3 } I came across two solutions: _.each(query, function removeUndefined(value, key) { if (_.isUndefined(value)) { delete query[key]; } }); or: _.omit(obj, _.filter(_.keys(obj), function(key) { return _.isUndefined(obj[key]) }