1

嗨,我目前遇到了检查具有另一个嵌套对象的对象的问题,如果其中的所有值都是 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 ,如果任何值不同于 null 或 0 ,则返回false

我不能使用:

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

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

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

无论如何要遍历对象并检查它的嵌套对象吗?

4

3 回答 3

1

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

于 2019-10-25T07:37:58.683 回答
1

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

于 2019-10-25T07:34:42.183 回答
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

于 2019-10-25T07:37:10.370 回答