0

I want to update field in json. My field is selectedCIID, this field is inside the some object with some hierarchy,

rows->panels->target->selectedCIId,

  var dashboard = results.dashboard;
  var dashboardJson = angular.fromJson(dashboard);
  //dashboardJson.rows //dashboardJson.rows[0].panels
  var j;
  for(i=0; i<=dashboardJson.rows; i++){
    for(j=0;)
    dashboardJson.rows[i].panels
    dashboardJson.rows[0].panels[0].targets[0].selectedCIID = ci;
    //dashboardJson.rows[0].panels[0].targets[0].selectedCIID
  }

JSON:

{
    "rows": [{
        "title": " row 1",
        "panels": [{
            "targets": [{
                "selectedCIID": "5856742957ce424b8db6cfb309b6b013",
                "series": ""
            }]
        }]
    }, {
        "title": "row 2",
        "panels": [{
            "targets": [{
                "selectedCIID": "5856742957ce424b8db6cfb309b6b013",
                "series": ""
            }]
        }, {
            "targets": [{
                "selectedCIID": "5856742957ce424b8db6cfb309b6b013",
                "series": ""
            }]
        }]
    }]
}

Code above is just dummy code for showing my approach. I can do this using native JS code. for loop.

But if I can use lodash for iterating this object _each or something it will be fine.

Or

How can I write the code to find selectedCIID inside panels wherever this field is coming updated with the my variable.

I want to update "selectedCIID" field. Please help me..

Javascript Coder
  • 5,691
  • 8
  • 52
  • 98

1 Answers1

0

If you want to change the property of all targets then multiple loops can do this.

var rowI, panelI, targetI,
    rows, panels, targets, target;

rows = dashboardJson.rows;
for (rowI = 0; rowI < rows.length; rowI++) {
    panels = rows[rowI].panels;

    for (panelI = 0; panelI < panels.length; panelI++) {
        targets = panels[panelI].targets;

        for (targetI = 0; targetI < targets.length; targetI++) {
            target = targets[targetI];
            target.selectedCIID  = ci;
        }   
    }   
}

You can also add some if statements, in case you want to filter desired rows, panels, targets

tenbits
  • 7,568
  • 5
  • 34
  • 53