最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript check all values of object and it's nested object - Stack Overflow

programmeradmin5浏览0评论

Hi i'm currently stuck with this problem of checking an object that has another nested object if all value in it is null or 0

My object is as follow:

{
   "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
}

i need to check every single values in it is 0 or null , return true if all values is either 0 or null, false if any of the values different than null or 0

i can't use :

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

It return False since the nested object still consider as a different value than 0 and null

I don't want to write super long if condition check every single value of it at a time

Is there anyway to iterate over the object and it's nested object to check ?

Hi i'm currently stuck with this problem of checking an object that has another nested object if all value in it is null or 0

My object is as follow:

{
   "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
}

i need to check every single values in it is 0 or null , return true if all values is either 0 or null, false if any of the values different than null or 0

i can't use :

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

It return False since the nested object still consider as a different value than 0 and null

I don't want to write super long if condition check every single value of it at a time

Is there anyway to iterate over the object and it's nested object to check ?

Share Improve this question edited Oct 25, 2019 at 7:48 Linh Nguyen asked Oct 25, 2019 at 7:31 Linh NguyenLinh Nguyen 3,9305 gold badges33 silver badges77 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You could take an iterative and recursive approach.

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

One (inelegant) option is to use JSON.stringify with a callback, and whenever a value other than 0 or null is found, set a flag:

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);

Or, to do it more manually, with short-circuiting:

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));

You can can create a function (fn) that uses Object.values() to get an array of values, iterate with Array.every() and if the value is an object use fn on it:

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)

发布评论

评论列表(0)

  1. 暂无评论