I can copy an object as follows:
var myNew = Object.assign({}, old);
But how do I do this and remove the undefined properties in old? For example:
const old = {1:undefined, 2: "TWO"};
const myNew = {2:"TWO"};
I can copy an object as follows:
var myNew = Object.assign({}, old);
But how do I do this and remove the undefined properties in old? For example:
const old = {1:undefined, 2: "TWO"};
const myNew = {2:"TWO"};
You could remove it manually: delete old['1']
Or create a method to do that for an unlimited number of keys.
var old = prune(old);
function prune(obj) {
var newObj = Object.assign({}, old);
for(var key in newObj) {
if(newObj[key] === undefined) {
delete newObj[key];
}
}
return newObj;
}