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]) }))
This question is related to
javascript
object
field
undefined
A one-liner using ES6 arrow function and ternary operator:
Object.keys(obj).forEach(key => obj[key] === undefined ? delete obj[key] : {});
Or use short-circuit evaluation instead of ternary: (@Matt Langlois, thanks for the info!)
Object.keys(obj).forEach(key => obj[key] === undefined && delete obj[key])
Same example using if statement:
Object.keys(obj).forEach(key => {
if (obj[key] === undefined) {
delete obj[key];
}
});
If you want to remove the items from nested objects as well, you can use a recursive function:
const removeEmpty = (obj) => {
let newObj = {};
Object.keys(obj).forEach((key) => {
if (obj[key] === Object(obj[key])) newObj[key] = removeEmpty(obj[key]);
else if (obj[key] !== undefined) newObj[key] = obj[key];
});
return newObj;
};
Mhh.. I think @Damian asks for remove undefined field (property) from an JS object
.
Then, I would simply do :
for (const i in myObj)
if (typeof myObj[i] === 'undefined')
delete myObj[i];
Short and efficient solution, in (vanilla) JS ! Example :
const myObj = {_x000D_
a: 1,_x000D_
b: undefined,_x000D_
c: null, _x000D_
d: 'hello world'_x000D_
};_x000D_
_x000D_
for (const i in myObj) _x000D_
if (typeof myObj[i] === 'undefined') _x000D_
delete myObj[i]; _x000D_
_x000D_
console.log(myObj);
_x000D_
One could also use the JQuery filter for objects
var newobj = $(obj).filter(function(key) {
return $(this)[key]!== undefined;
})[0];
Demo here
I prefer to use something like Lodash:
import { pickBy, identity } from 'lodash'
const cleanedObject = pickBy(originalObject, identity)
Note that the identity function is just x => x
and its result will be false for all falsy values. So this removes undefined, "", 0, null, ...
If you only want the undefined
values removed you can do this:
const cleanedObject = pickBy(originalObject, v => v !== undefined)
It gives you a new object, which is usually preferable over mutating the original object like some of the other answers suggest.
This solution also avoids hasOwnProperty()
as Object.keys returns an array of a given object's own enumerable properties.
Object.keys(obj).forEach(function (key) {
if(typeof obj[key] === 'undefined'){
delete obj[key];
}
});
and you can add this as null
or ''
for stricter cleaning.
var obj = { a: 1, b: undefined, c: 3 }
To remove undefined
props in an object we use like this
JSON.parse(JSON.stringify(obj));
Output: {a: 1, c: 3}
This one is easy to remember, but might be slow. Use jQuery to copy non-null properties to an empty object. No deep copy unless you add true
as first argument.
myObj = $.extend({}, myObj);
Because it doesn't seem to have been mentioned, here's my preferred method, sans side effects or external dependencies:
const obj = {_x000D_
a: 1,_x000D_
b: undefined_x000D_
}_x000D_
_x000D_
const newObject = Object.keys(obj).reduce((acc, key) => {_x000D_
const _acc = acc;_x000D_
if (obj[key] !== undefined) _acc[key] = obj[key];_x000D_
return _acc;_x000D_
}, {})_x000D_
_x000D_
console.log(newObject)_x000D_
// Object {a: 1}
_x000D_
Here's a plain javascript (no library required) solution:
function removeUndefinedProps(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop) && obj[prop] === undefined) {
delete obj[prop];
}
}
}
Working demo: http://jsfiddle.net/jfriend00/djj5g5fu/
Another Javascript Solution
for(var i=0,keys = Object.keys(obj),len=keys.length;i<len;i++){
if(typeof obj[keys[i]] === 'undefined'){
delete obj[keys[i]];
}
}
No additional hasOwnProperty
check is required as Object.keys
does not look up the prototype chain and returns only the properties of obj
.
Source: Stackoverflow.com