How to check if object is empty in javascript for all levels in object

本小妞迷上赌 提交于 2020-11-28 07:56:05

问题


I wanted to find out if my object is empty or not for all its nested objects and key-value pairs.

for e.g.,

const x = {
  a:"",
  b:[],
  c:{
    x:[]
  },
  d:{
    x:{
      y:{
        z:""
      }
    }
  }
};

this should be an empty object and if any of this contains single value then it should be non empty.


回答1:


Here is the way to do what using recursion

const x = {
  a:"",
  b:[],
  c:{
    x:[]
  },
  d:{
    x:{
      y:{
        z:''
      }
    }
  }
};
function checkEmpty(obj){
  
  for(let key in obj){
    //if the value is 'object'
    if(obj[key] instanceof Object === true){
      if(checkEmpty(obj[key]) === false) return false;
    }
    //if value is string/number
    else{
      //if array or string have length is not 0.
      if(obj[key].length !== 0) return false;
    }
  }
  return true;
}
console.log(checkEmpty(x))
x.d.x.y.z = 0;
console.log(checkEmpty(x));



回答2:


You can write a recursive function like following. Function creates a set with 2 possible values true and false. If the size of set is 1 and the value being false, which mean that the object is empty.

const x = {a:"",b:[],c:{x:[]},d:{x:{y:{z:""}}}};

function isEmpty(o, r = new Set()) {
  for (let k in o) {
    if(typeof o[k] === "object") {
      if(Array.isArray(o[k])) r.add(!!o[k].length);
      else isEmpty(o[k],r);
    } else r.add(!(o[k] === "" || o[k] === undefined || o[k] === null));
  }
  return r;
}
let result = isEmpty(x);

console.log(result.has(false) && result.size == 1);



回答3:


I will use a recursive approach for this one, we iterate over the object.keys() and check is every value related to the key is empty, in the case the value is an object, we go one level deeper to check it.

const x = {
  a:"",
  b:[],
  c:{x:[]},
  d:{x:{y:{z:""}}}
};

const x1 = [0,0,0];
const x2 = {0:0,1:0,2:0};

const isEmpty = (obj, empty=true) =>
{
    Object.keys(obj).forEach((key) =>
    {
        if (typeof obj[key] === "object")
            empty = isEmpty(obj[key], empty);
        else
            empty = empty && (obj[key].length === 0);
            
        // Return early if we detect empty here.
        
        if (!empty) return empty;
    });
    
    return empty;
}

console.log("original x: ", isEmpty(x));

x.a = "I'm not empty";

console.log("x after edit: ", isEmpty(x));
console.log("x1: ", isEmpty(x1));
console.log("x2: ", isEmpty(x2));



回答4:


try (we use here recursion, fat arrow, obj. keys, reduce, ternary operator and object checking)

let isEmpty = o => o.constructor.name === "Object" ? 
  Object.keys(o).reduce((y,z)=> y&&isEmpty(o[z]) ,true) : o.length == 0;

const x = {
  a:"",
  b:[],
  c:{
    x:[]
  },
  d:{
    x:{
      y:{
        z:""
      }
    }
  }
};


let isEmpty = o => o.constructor.name === "Object" ? 
    Object.keys(o).reduce((y,z)=> y&&isEmpty(o[z]) ,true) : o.length == 0;
  


// Test
console.log(isEmpty(x));
x.d.x.y.z="Smile to life and life will smile to you";
console.log(isEmpty(x));


来源:https://stackoverflow.com/questions/54643055/how-to-check-if-object-is-empty-in-javascript-for-all-levels-in-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!