Javascript 检查对象的所有值及其嵌套对象

Javascript check all values of object and it's nested object

嗨,我目前遇到了这个问题,如果其中的所有值都为 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,return true 如果所有值都是 0 或 null,false 如果任何值不同于 null 或 0

我不会用:

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

它 return False 因为嵌套对象仍然被视为与 0 和 null

不同的值

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

是否有遍历对象及其嵌套对象来检查?

一个(不优雅的)选项是将 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));

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

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

您可以创建一个函数 (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)