Linh Nguyen
Linh Nguyen

Reputation: 3890

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

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 ?

Upvotes: 3

Views: 2961

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386680

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

Upvotes: 3

Ori Drori
Ori Drori

Reputation: 192287

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)

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370989

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

Upvotes: 2

Related Questions