【问题标题】:Javascript check all values of object and it's nested objectJavascript检查对象的所有值及其嵌套对象
【发布时间】:2020-02-21 13:04:57
【问题描述】:

您好,我目前遇到的问题是检查一个对象是否包含另一个嵌套对象,如果其中的所有值都是 null 或 0

我的对象如下:

{
   "city":0,
   "road":{
      "max":null,
      "min":null
   },
   "size":{
      "max":null,
      "min":null
   },
   "type":null,
   "ward":0,
   "floor":null,
   "price":{
      "max":null,
      "min":null
   },
   "street":0,
   "toilet":null,
   "balcony":null,
   "bedroom":null,
   "district":0,
   "frontend":{
      "max":null,
      "min":null
   },
   "direction":null,
   "living_room":null
}

我需要检查其中的每个值是 0 还是 null,如果所有值都是 0 或 null,则返回 true,如果任何值不同于空或 0

我不能使用:

Object.values(object).every(i => (i === null || i === ''))

它返回 False,因为嵌套对象仍然认为是不同于 0 和 null 的值

如果条件一次检查它的每一个值,我不想写超长

是否有遍历对象和它的嵌套对象来检查?

【问题讨论】:

    标签: javascript nested-object


    【解决方案1】:

    您可以采用迭代和递归的方法。

    function check(object) {
        return Object.values(object).every(v => v && typeof v === 'object'
            ? check(v)
            : v === 0 || v === null
        );
    }
    
    var data0 = { city: 0, road: { max: null, min: null }, size: { max: null, min: null }, type: "sell", ward: 0, floor: null, price: { max: null, min: null }, street: 0, toilet: null, balcony: null, bedroom: null, district: 0, frontend: { max: null, min: null }, direction: null, living_room: null },
        data1 = { city: 0, road: { max: null, min: null }, size: { max: null, min: null }, type: null, ward: 0, floor: null, price: { max: null, min: null }, street: 0, toilet: null, balcony: null, bedroom: null, district: 0, frontend: { max: null, min: null }, direction: null, living_room: null };
    
    console.log(check(data0)); // false because of type: "sell"
    console.log(check(data1)); // true

    【讨论】:

    • 如果每个 v 等于 null 或 0 ,它不应该返回 true 吗?还是我弄错了?
    • 对,确实如此。但你有type: "sell",它不是null 或零。
    • 哦,是的,我忘了编辑那个,输入应该为空
    【解决方案2】:

    您可以创建一个函数 (fn),该函数使用 Object.values() 获取值数组,使用 Array.every() 进行迭代,如果该值是对象,则在其上使用 fn

    const fn = data =>
      Object.values(data)
      .every(v => {
        if(v === null || v === 0) return true;
        
        return typeof v === 'object' ? fn(v) : false;
      })
    
    const data = {"city":0,"road":{"max":null,"min":null},"size":{"max":null,"min":null},"type":"sell","ward":0,"floor":null,"price":{"max":null,"min":null},"street":0,"toilet":null,"balcony":null,"bedroom":null,"district":0,"frontend":{"max":null,"min":null},"direction":null,"living_room":null}
    
    const result = fn(data)
    
    console.log(result)

    【讨论】:

      【解决方案3】:

      一个(不优雅的)选项是将JSON.stringify 与回调一起使用,并且每当找到除 0 或 null 以外的值时,设置一个标志:

      const obj = {
         "city":0,
         "road":{
            "max":null,
            "min":null
         },
         "size":{
            "max":null,
            "min":null
         },
         "type":"sell",
         "ward":0,
         "floor":null,
         "price":{
            "max":null,
            "min":null
         },
         "street":0,
         "toilet":null,
         "balcony":null,
         "bedroom":null,
         "district":0,
         "frontend":{
            "max":null,
            "min":null
         },
         "direction":null,
         "living_room":null
      };
      
      let allZeroNull = true;
      JSON.stringify(obj, (key, val) => {
        if (typeof val !== 'object' && val !== 0) {
          allZeroNull = false;
        }
        return val;
      });
      console.log(allZeroNull);

      或者,更手动地,通过短路:

      const obj = {
         "city":0,
         "road":{
            "max":null,
            "min":null
         },
         "size":{
            "max":null,
            "min":null
         },
         "type":"sell",
         "ward":0,
         "floor":null,
         "price":{
            "max":null,
            "min":null
         },
         "street":0,
         "toilet":null,
         "balcony":null,
         "bedroom":null,
         "district":0,
         "frontend":{
            "max":null,
            "min":null
         },
         "direction":null,
         "living_room":null
      };
      
      const isAllZeroNull = (item) => {
        if (typeof item === 'object' && item !== null) {
          for (const val of Object.values(item)) {
            if (!isAllZeroNull(val)) {
              return false;
            }
          }
        } else if (item !== 0 && item !== null) {
          return false;
        }
        return true;
      };
      console.log(isAllZeroNull(obj));

      【讨论】:

      • 这……很愚蠢但令人印象深刻,值得一票。
      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2022-01-15
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 2017-06-18
      相关资源
      最近更新 更多