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