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

8
  • It's fine just a problem in condition(i<=dashboardJson.rows) remove equality = check from this condition as you are starting from 0 index not 1; Commented Mar 21, 2016 at 7:06
  • yes. I got it. this just and example.. can you suggest me same way to do in lodash.. Commented Mar 21, 2016 at 7:07
  • Try this Loop through properties in JavaScript object with Lodash Commented Mar 21, 2016 at 7:09
  • I want to find key and want to update in all the places, that key can come many time.. Commented Mar 21, 2016 at 7:16
  • You can update key value for current row iterating; Commented Mar 21, 2016 at 7:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.