I'm wanting to remove empty objects from a nested JSON. I've check a fair few answers on SA but none of them are doing what I need. any assistance is appreciated. I thought that I might be able to reduce the object which is working but I had to do this Object.keys(obj || {}) to stop Object.keys throwing type errors on undefined objects. I'm just wanting to remove any objects that are empty {} and return the objects with values.
Thanks in advance. Jimi
!--- sample object
export const stocklevels = {
coats: {
browncoat: ["L", "M", "S"]
},
neclace: {},
earrings: {
diamond: "Y",
ruby: "Y"
},
shoes: {},
bags: {}
};
!--- function
let newObj = Object.keys(obj || {}).reduce((x, k) => {
if (obj[k] != null) {
x[k] = obj[k];
}
return x;
}, {});
return newObj;
null. Soobj[k] != nullis going to betruefor all keys instocklevels.