0

I have an input array which looks like this:-

const x = [
    {
        "id": "neha-1g5te3",
        "value": "",
    },
    {
        "rules": [
            {
                "rules": [
                    {
                        "id": "neha-1xer23",
                        "value": "",
                    },
                    {
                        "rules": [],
                        "combinator": "AND"
                    }
                ],
                "combinator": "AND"
            }
        ],
        "combinator": "AND"
    },
    {
        "rules": [],
        "combinator": "AND"
    }
]

This is a value which gets created by react-query-builder. I can have nested rules inside one rules object. I want to check for each rules whether it has a value or not. I want to have a flag where

if(rules.length === 0){ flag = false}

and also,

 if(rules.value === '') { flag = false}

I'm unable to loop through every rules object present inside x

Like in the above case, we have 2 outside rules, and total 4 rules object. I want to check the above two conditions on all the rules

I tried by doing

x?.rules?.every((e) => e.value !== '')

but this doesn't work for nested rules

Note: This isn't similar to this question as here rules object can have multiple rules object inside it.

If I use the function

function getValue(o, findKey) {
  const output = []
  
  for (const k in o) {
    if (k === findKey)
      output.push(o[findKey])
    else if (typeof o[k] === 'object')
      output.push(...getValue(o[k], findKey))
  }
  
  return output;
}
console.log(getValue(x, 'rules'))

O/P: [ [ { rules: [Array], combinator: 'AND' } ], [] ]

If you see in above case, it shows us [Array] in output. I want to go inside this Array and check for more rules object if there are any

Neha Chaudhary
  • 1,071
  • 4
  • 15
  • 31

1 Answers1

1

You can achieve this with recursion. I used an external variable though to store the result.

If false, then this means that an empty rules was found.

const x = [
    {
        "id": "neha-1g5te3",
        "value": "",
    },
    {
        "rules": [
            {
                "rules": [
                    {
                        "id": "neha-1xer23",
                        "value": "",
                    },
                    {
                        "rules": [],
                        "combinator": "AND"
                    }
                ],
                "combinator": "AND"
            }
        ],
        "combinator": "AND"
    },
    {
        "rules": [],
        "combinator": "AND"
    }
]

let result = true;

const checkRules = obj => {
  for (const [key, value] of Object.entries(obj)) {
    if (key === 'rules' && Array.isArray(value) && value.length === 0) {
      result = false;
      break;
    } else {
      if (typeof value === 'object' || Array.isArray(value)) {
         checkRules(value)
      }
    }     
  }
 
}

checkRules(x)
console.log(result);
Apostolos
  • 10,033
  • 5
  • 24
  • 39