Skip to Content

how to combine values of a json array?

I have json array like this.. :

{

mycollection : [

{ module : "SD", severity : "high", count : 1 },

{ module : "SD", severity : "veryhigh", count : 1 },

{ module : "SD", severity : "critical", count : 1 },

{ module : "SD", severity : "high", count : 1 },

{ module : "SD", severity : "veryhigh", count : 1 },

{ module : "FICO", severity : "high", count : 1 },

{ module : "FICO", severity : "veryhigh", count : 1 },

{ module : "FICO", severity : "high", count : 1},

{ module : "FICO", severity : "veryhigh", count : 1 },

{ module : "FICO", severity : "critical", count : 1 }

]

};

i need output like below iam using javascript(sapui5) is there any way to achieve this?? :

{

mycollection : [

{ module : "SD", severity : "high", count : 2 },

{ module : "SD", severity : "veryhigh", count : 2 },

{ module : "SD", severity : "critical", count : 1 },

{ module : "FICO", severity : "high", count : 2 },

{ module : "FICO", severity : "veryhigh", count : 2 },

{ module : "FICO", severity : "critical", count : 1 }

]

};

plz Help

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Dec 05, 2014 at 12:14 PM

    Hi Rajeesh,

    Please create another array myCollectionNew and a variable myCollectStrcut {module:"",severity"",count:""} (P.S. you have to make myCollectStruct.length = 0 after its defination to remove the first empty element.)

    Add the first element of the myCollection to myCollectionNew using the myCollectSTruct variable. (You can add the variable to the array through push method).

    Now traverse through the myCollection comparing element 1 and 2 through the end of the array(nested loop). When ever there is an match(a combination of module and severity), increase the count in myCollectionNew and if the element is new add the new element(using the myCollectStruct) to the myCollectionNew.

    You will have the desired output in myCollectionNew.

    Regards,

    Jagan

    Add a comment
    10|10000 characters needed characters exceeded

    • I found a solution with your help thanks...

      var oDataModel=new sap.ui.model.odata.ODataModel("odataurl", true);

      var items;

      oDataModel.read("/WorkflowTaskCollection?$format=json&$orderby=workitem_id desc", null, null, false,function success(oData){

      items=oData.results.length;

      var jsonDashboard = {"mycollection":[]};

      //var jnew = {"mycollectionnew":[]};

      var data = oData.results;

      //a var data1 = jsonDashboard.mycollection;

      for (i = 0; i < data.length; i++)

      {

      var subject = data[i].subject.split("^^");

      //alert(subject[1]);

      var x = subject[0];

      var y = subject[1];

      var z = 1;

      if (jsonDashboard.mycollection.length == 0){

      jsonDashboard.mycollection.push({"module":x, "severity":y,"count":z});

      }

      else {

      for(j = 0; j < jsonDashboard.mycollection.length; j++)

      {

      if(jsonDashboard.mycollection[j].module == x )

      {

      if(jsonDashboard.mycollection[j].severity == y){

      jsonDashboard.mycollection[j].count = jsonDashboard.mycollection[j].count + 1;

      var flag = 0;

      break;

      }

      }

      else{

      //if(!(jsonDashboard.mycollection[j].module == x) && !(jsonDashboard.mycollection[j].severity == y)){

      var flag = 1;

      // }

      //jsonDashboard.mycollection.push({"module":x, "severity":y,"count":z});}

      }

      }

      if (flag == 1){

      jsonDashboard.mycollection.push({"module":x, "severity":y,"count":z});

      }

      }

      }

      }, function failure(){employeeNo=1000;});

  • Posted on Dec 08, 2014 at 12:59 PM

    Hi Rajeesh,

    the Lo-Dash library is an excellent helper for these kind of problems.

    Unfortunately I can't give you the complete solution of your problem right now. Maybe these functions will help you on your path to the solution ;-)

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.