简体   繁体   English

如何检查嵌套 object 中的值是否为空

[英]How to check if value is empty in nested object

For condition rendering, i'm checking if my object get an empty value.对于条件渲染,我正在检查我的 object 是否得到一个空值。

I'm using "useState" to pass each object value to "isEmpty".我正在使用“useState”将每个 object 值传递给“isEmpty”。 First I'm creating a new object because i delete the value " SummaryOfChanges" who can be empty.首先,我正在创建一个新的 object,因为我删除了可以为空的值“SummaryOfChanges”。 After, I'm using "some" to get every value and pass every value to 'isEmpty'之后,我使用“一些”来获取每个值并将每个值传递给“isEmpty”

//USE EFFECT //使用效果

 useEffect(() => {
    if (offerData) {
      let myObject = { ...offerData };
      const { SummaryOfChanges, ...newObject } = myObject;
      
      if (Object.values(newObject)?.some((x) => isEmpty(x))) {
        setCheckIfEmpty(true);
      } else {
        setCheckIfEmpty(false);
      }
    }
  }, []);

//ISEMPTY //是空的

export const isEmpty = (value) => {
  return (
    value === undefined ||
    value === null ||
    (typeof value === 'object' && Object.keys(value).length === 0) ||
    Object.keys(value) === undefined ||
    Object.keys(value) === null ||
    (typeof value === 'string' && value.trim().length === 0)
  );
};

exemple of my object:我的 object 的例子:

offerData : {
  valueOne : "hello",
  objectOne : { 
    valueOne: 'Hello', 
    valueTwo : null}
}

problem: isEmpty work perfectly and check in my object if value, nested object or array are empty.问题:isEmpty 工作完美,如果值、嵌套的 object 或数组为空,请检查我的 object。 But sometimes, nested object is not empty but have value "null".但有时,嵌套的 object 不是空的,而是值为“null”。

I need to add in my "isEmpty" some condition to check each value in nested object and array.我需要在我的“isEmpty”中添加一些条件来检查嵌套的 object 和数组中的每个值。

So, in this situation, setCheckIfEmpty will return 'false' because my "objectOne" is not empty, even if "objectOne.valueTwo" === null.因此,在这种情况下,setCheckIfEmpty 将返回“false”,因为我的“objectOne”不为空,即使“objectOne.valueTwo”=== null。

I'm actually trying to map through each value, but for the moment it is not working.我实际上正在尝试通过每个值 map,但目前它不起作用。

To recursively check objects, you should modify your isEmpty function to call itself on nested objects:要递归检查对象,您应该修改isEmpty function 以在嵌套对象上调用自身:

export const isEmpty = (value) => {
  if (typeof value === 'object' && value !== null) {
    for (const v of Object.values(value)) {
      if (isEmpty(v)) return true;
    }
  }

  return (
    value === undefined ||
    value === null ||
    (typeof value === 'object' && Object.keys(value).length === 0) ||
    // Also these lines aren't needed because Object.keys() always returns an array
    // Object.keys(value) === undefined ||
    // Object.keys(value) === null ||
    (typeof value === 'string' && value.trim().length === 0)
  );
};

To search inside an object's values or in any children, however deep inside the children might be, you can use recursion:要在对象的值或任何子项中搜索,无论子项内部有多深,您都可以使用递归:

// Return true if object has a null, undefined, empty string or empty object value or if any children (however deep inside the children might be) has these value:
function isEmpty(myObject) {
  for(var key in myObject) {
    if(myObject[key] == null || myObject[key] == undefined) return true;
    if((typeof myObject[key] === 'object' && Object.keys(myObject[key]).length === 0)) return true;
    if((typeof myObject[key] === 'string' && myObject[key].trim().length === 0)) return true;
    if(myObject[key] instanceof Object) return isEmpty(myObject[key]);
  }
  
  return false;
}

Testing:测试:

let o = {
  valueOne : "hello",
  objectOne : { 
    valueOne: 'Hello', 
    valueTwo : null}
}


isEmpty(o); //true

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

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