简体   繁体   English

如何检查对象中所有级别的JavaScript中对象是否为空

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

I wanted to find out if my object is empty or not for all its nested objects and key-value pairs. 我想找出所有嵌套对象和键值对的对象是否为空。

for eg, 例如

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. 这应该是一个空对象,并且如果其中任何一个包含单个值,则它应该为非空。

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)) xdxyz = 0; console.log(checkEmpty(x)); 

You can write a recursive function like following. 您可以编写如下的递归函数。 Function creates a set with 2 possible values true and false. 函数创建一个具有两个可能值true和false的集合。 If the size of set is 1 and the value being false, which mean that the object is empty. 如果set的大小为1并且值为false,则表示该对象为空。

 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); 

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. 我将为此使用一种递归方法,我们遍历object.keys()并检查与键相关的每个值是否为空,如果该值是一个对象,我们将进行更深入的检查。

 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)); xa = "I'm not empty"; console.log("x after edit: ", isEmpty(x)); console.log("x1: ", isEmpty(x1)); console.log("x2: ", isEmpty(x2)); 

try (we use here recursion , fat arrow , obj. keys , reduce , ternary operator and object checking ) 试试(我们在这里使用递归粗箭头对象键reduce三元运算符对象检查

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)); xdxyz="Smile to life and life will smile to you"; console.log(isEmpty(x)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM