【问题标题】:check is an object is fully empty检查对象是否完全为空
【发布时间】:2022-12-08 00:25:12
【问题描述】:

此对象具有属性,但没有值,或者至少是空数组。我想做一个条件检查对象测试是否在数组中没有内容然后返回 true。

const test = {
  "10": [],
  "1": []
};

const output = Object.entries(cl).every(element => Object.values(element).some(val =>  val === "");

console.log(output);

【问题讨论】:

  • 这是否应该包括检查不是数组或空值的属性?如果是,那么使用长度的答案将导致异常。

标签: javascript reactjs arrays object


【解决方案1】:

与其他答案类似,增加了检查空对象的功能。

const pass1 = {};
const pass2 = { 123: [] }
const pass3 = { 123: [], 234: [] }
const fail1 = { 123: [], 234: [1] }

const isEmpty = (o) => !Object.keys(o).length || Object.values(o).every(a => !a.length);

[pass1,pass2,pass3,fail1].forEach(item => console.log(isEmpty(item)));

0是假的,所以a.length就够了,你不需要检查a.length == 0

【讨论】:

    【解决方案2】:

    只需更改 some() 中的代码即可。检查数组长度

    const output = Object.entries(clientData).every(element => Object.values(element).some(val => Array.isArray(element) && val.length == 0);
    
    const test = {
        "106596": [],
        "107014": []
         };
    
    
    Output : false
    

    当在测试元素的数组中添加一个元素时;

    const test = {
         "106596": [1],
         "107014": []
        };
    
    
    Output : true
    

    【讨论】:

    • 如果属性不是数组,这会起作用吗?这个问题似乎包括所有可能的数据类型。
    【解决方案3】:

    如您所见,有很多方法可以实现此功能,下面是我自己的方法,代码中有解释性的 cmets:

    const test = {
      "106596": [],
      "107014": []
    },
    // we declare a function to check if the array-values are empty,
    // we pass in the Object we're testing, and from we use
    // Object.values() to retrieve the values of the Object; we
    // then iterate over the array-values using Array.prototype.every():
    arrayValuesEmpty = (obj) => Object.values(obj).every(
      // along with an anoymous Arrow function to check if the
      // current array-element value ('val') is an Array (using
      // Array.isArray() and that its length property is exactly zero:
        (val) => Array.isArray(val) && val.length === 0
    );
    
    console.log(arrayValuesEmpty(test));

    上面确实有一些天真的假设,即所有对象值都是数组,并假设传入的参数将是一个具有属性的对象;为了防止这些假设:

    const test = {
        // adding new properties, String, Number and Boolean:
        stringProperty: 'Steven',
        numericalProperty: 3,
        booleanProperty: false,
        "106596": [],
        "107014": []
      },
      // here we pass in a default value (empty Object) to the function if
      // the user supplies no argument:
      arrayValuesEmpty = (obj = {}) => {
        // here we retrieve a two-dimensional Array of all Object entries,
        // in [key, value] arrays:
        const entries = Object.entries(obj);
    
        // if the length of entries in the Array of entries is zero we have
        // an empty Object (so no point continuing):
        if (entries.length === 0) {
          // returning false from the function:
          return false;
        }
       // otherwise we use Array.prototype.map() create a new Array:
       return entries.map(
        // using destructuring to assign the first array-element
        // to the (unused) 'key' variable, and the second (property-value
        // for the current entry) to the 'val' variable; and return that
        // 'val' (property-value of the original Object):
        ([key,val]) => val)
          // we then filter that Array of values using Array.prototype.filter():
          .filter(
            // passing the val variable to the function (the property-value of
            // the original Object) to return a new Array of Object-values
            // which are Arrays (checking with Array.isArray):
            (val) => Array.isArray(val)
          // then iterating over the Array-values using Array.prototype.every():
          ).every(
            // here we're checking if the Array-length is exactly zero:
            (val) => val.length === 0
          // if every Array-element is empty, so the length of every Array-element
          // is exactly zero then Array.prototype.every() returns Boolean true,
          // which is returned from the function:
          );
      };
    
    console.log(arrayValuesEmpty(test));

    参考:

    【讨论】:

    • 在考虑所有可能的结果之前,这绝对是一开始看起来很简单的问题之一。 +1
    • 谢谢!是的,简单的准系统实现非常简单,但边缘情况会增加复杂性;尽管我当然没有涵盖所有可能的问题,毕竟:它应该处理对象的 JSON 字符串吗?如果传递的是数组而不是对象?这些很容易构建,但除非 OP 决定要求进一步的复杂性,否则我很乐意暂时保持原样 :)
    【解决方案4】:

    检查数组和非 null / undefine 的选项可以像这样使用 object.values & filter

    对象.值: 返回对象的所有值

    const is_empty : boolean = (item) => !Object.values(item).filter(n => (Array.isArray(n) && n.length > 0) || n === null || n === undefined ).length > 0
    

    【讨论】:

      【解决方案5】:

      你可以通过Object.values获取values,然后循环检查value是否为数组,然后检查数组的长度是否为零,如果不等于则检查值是否等于null或@987654325 @

      const test = {
        "106596": [],
        "107014": [],
      };
      
      const test2 = {
        "106596": [5],
        "107014": [],
      };
      
      const test3 = {
        "106596": [],
        "107014": [],
        "121123": 'something',
      };
      
      
      function checkEmpty(value) {
        return Object.values(value).every(element => Array.isArray(element) ? element.length === 0 : [null, undefined].includes(element));
      }
       
      
      console.log(checkEmpty(test));  // true
      console.log(checkEmpty(test2)); // false
      console.log(checkEmpty(test3)); // false

      【讨论】:

        【解决方案6】:
          const test = {
            "106596": [],
            "107014": []
          };
          
          const getKeys = Object.keys(test);
          const checkVal = getKeys.map((item)=> test.[item].length);
          const result = checkVal.some(e => e)
          console.log(result)
        

        你可以一步完成这个而不是这个,希望这会有所帮助

        【讨论】:

          【解决方案7】:

          const test = {
            one: [],
            two: [],
            three: undefined,
            hasVal: "Has Value!!",
            nullVal: null,
          };
          
          // loop
          // you can ignore key
          for (const [key, val] of Object.entries(test)) {
            // condintion
            if (val !== null && val !== undefined && val.length != 0) {
              console.log(true);
            }
          }

          【讨论】:

          • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写出好的答案的信息in the help center
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-11
          • 2014-01-01
          相关资源
          最近更新 更多