简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM