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

时间:2019-10-25 07:31:37

标签: javascript 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,如果所有值均为0或null则返回 true ,如果其中任何一个不同于以下值,则返回 false null或0

我不能使用:

  

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

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

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

无论如何,有没有要遍历该对象及其嵌套对象以进行检查?

3 个答案:

答案 0 :(得分: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));

答案 1 :(得分: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

答案 2 :(得分: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)