[英]Loop through nested Json javascript
I'm trying to loop through all the conditions for a form logic system utilizing the json-logic-js library and having issues going into the infinite nested conditions.我正在尝试使用 json-logic-js 库遍历表单逻辑系统的所有条件,并且遇到进入无限嵌套条件的问题。 It works when there is no additional nested operations but can't figure out away to loop through and detect if the condition contains additional conditions infinitely..
当没有额外的嵌套操作但无法找出循环并检测条件是否无限包含额外条件时,它可以工作..
What I'm needing is this output我需要的是这个输出
{
"or": [
{
"==": [
"0hxsj03jab9pjsu1",
"Rig 15"
]
},
{
"or": [
{
"==": [
"5l12cnsnxe1911bm",
"Corporate"
]
},
{
"==": [
"5l12cnsnxe1911bm",
"Pumping"
]
}
]
},
{
"and": [
{
"==": [
"69vsm5bkfb101n2n",
"Unsafe"
]
},
{
"or": [
{
"==": [
"b09ivo9r1aldu6jf",
"Yes"
]
},
{
"==": [
"0hxsj03jab9pjsu1",
"Rig 44"
]
},
{
"==": [
"0hxsj03jab9pjsu1",
"Other"
]
}
]
}
]
}
]
}
I currently have this much.我目前有这么多。
var item = { "id":"8wj4xhwe3pd9aage", "showif": { "operator": "or", "conditions": [ { "operator": "and", "conditions": [ { "data": { "fieldId": "69vsm5bkfb101n2n", "settings": { "values": [ "Unsafe" ] } } }, { "operator": "or", "conditions": [ { "data": { "fieldId": "b09ivo9r1aldu6jf", "settings": { "values": [ "Yes" ] } } }, { "data": { "fieldId": "0hxsj03jab9pjsu1", "settings": { "values": [ "Rig 44", "Other" ] } } } ] } ] }, { "data": { "fieldId": "0hxsj03jab9pjsu1", "settings": { "values": [ "Rig 15" ] } } }, { "data": { "fieldId": "5l12cnsnxe1911bm", "settings": { "values": [ "Corporate", "Pumping" ] } } } ] } }; var condition = []; item.showif.conditions.forEach(element => { var orC = [] if(element.data){ element.data.settings.values.forEach(values => { if(element.data.settings.values.length <= 1){ condition.push({"==": [element.data.fieldId,values]}) }else{ orC.push({"==": [element.data.fieldId,values]}) } }); if(orC.length >= 1){ condition.push({"or":orC}) } } }); var Logic = {[item.showif.operator]: condition} console.log(Logic)
I cant wrap my brain around this without getting lost..我无法在不迷路的情况下将我的大脑环绕在此周围。
I figured it out.我想到了。 For anyone else with this here is my code :)
对于其他人,这里是我的代码:)
var item = { "id": "8wj4xhwe3pd9aage", "showif": { "operator": "or", "conditions": [ { "operator": "and", "conditions": [ { "data": { "fieldId": "69vsm5bkfb101n2n", "settings": { "values": [ "Unsafe" ] } } }, { "operator": "or", "conditions": [ { "data": { "fieldId": "b09ivo9r1aldu6jf", "settings": { "values": [ "Yes" ] } } }, { "data": { "fieldId": "0hxsj03jab9pjsu1", "settings": { "values": [ "Rig 44", "Other" ] } } } ] } ] }, { "data": { "fieldId": "0hxsj03jab9pjsu1", "settings": { "values": [ "Rig 15" ] } } }, { "data": { "fieldId": "5l12cnsnxe1911bm", "settings": { "values": [ "Corporate", "Pumping" ] } } } ] } }; function loopThrough(obj) { var operator = obj.operator var condition = []; obj.conditions.forEach(element => { var orC = [] if (element.data) { element.data.settings.values.forEach(values => { if (element.data.settings.values.length <= 1) { condition.push({ "==": [element.data.fieldId, values] }) } else { orC.push({ "==": [element.data.fieldId, values] }) } }); if (orC.length >= 1) { condition.push({ "or": orC }) } } else if (element.operator) { condition.push(this.loopThrough(element)) } }); return ({ [operator]: condition }) } console.log(loopThrough(item.showif));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.