0

Hi iam trying to group my json data in to sub json data. here is my json data

        [
              {
                  "STATUS":"ACTIVE",
                  "AMOUNT":200,
                  "pENDING":100,
                },
                {
                  "STATUS":"NOTACTIVE",
                  "AMOUNT":100,
                  "pENDING":30,
                },
                {
                  "STATUS":"NOTACTIVE",
                  "AMOUNT":150,
                  "pENDING":10,
                }
          ]

and my expected result like

                 [
                     {
                     "STATUS":"ACTIVE",
                     "COUNT":"1",
                      "TOTAL AMOUNT":200,
                       "TOTAL PENDING":100
                      },
                      {
                      "STATUS":"NOTACTIVE",
                      "COUNT":"2",
                      "TOTAL AMOUNT":250,
                      "TOTAL PENDING":40
                      }
                 ]

I want the separate count ,sum of amount,sum of pending for each status Could you please help me to find the result

5
  • 1
    what you have tried ? Commented Dec 13, 2018 at 16:52
  • const result= {}; data.forEach(entry => { if(!result[entry.STATUS]){ result[entry.STATUS] = 0; } result[entry.STATUS]+= entry.AMOUNT; }); console.log(result) Commented Dec 13, 2018 at 16:54
  • Possible duplicate of how to count same object attribute values in array Commented Dec 13, 2018 at 16:54
  • but how can i get sum of two object along with count Commented Dec 13, 2018 at 16:56
  • @user5466590 Post what you have tried already into the question. Doesn't matter if it's wrong. Commented Dec 13, 2018 at 17:04

3 Answers 3

0

You can do it like this

Idea to handle such things.

so here i am looping through the array and checking for status in the output object. if the status is already in output object i will update the required values. if it's not there than i will create a new one in output object.

let arr =[
           {
            "STATUS":"ACTIVE",
            "AMOUNT":200,
            "PENDING":100,
           },
           {
            "STATUS":"NOTACTIVE",
            "AMOUNT":100,
            "PENDING":30,
           },
           {
            "STATUS":"NOTACTIVE",
            "AMOUNT":150,
            "PENDING":10,
           }
          ];
let output = arr.reduce((op,cur)=>{
  if(op[cur['STATUS']]){
    op[cur['STATUS']]['TOTAL_AMOUNT']+=cur['AMOUNT'];
    op[cur['STATUS']]['TOTAL_PENDING']+=cur['PENDING'];
    op[cur['STATUS']]['COUNT']++;
  } else {
       op[cur['STATUS']] ={
        'COUNT' : 1,
        'TOTAL_AMOUNT' : cur['AMOUNT'],
        'TOTAL_PENDING' : cur['PENDING'],
       }
  }
return op;
},{})
console.log(output);

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

Comments

0

Trying to fix your own method I would suggest something like this. I tried to do it generic in case you would find it more useful, although this solution would have a problem if the AMOUNT or PENDING entries are not numeric.

const IDENTIFIER = 'STATUS' 
const result= {}; 

data.forEach( entry => { 

    //  Check is this status exists, if not, create one
    if(!result[entry[IDENTIFIER]]){ 
        result[entry[IDENTIFIER]] = {}
    } 

    //  For each item in the entry add the value
    Object.keys(entry).forEach( item => {
        //  Only sum the items that are not the identifier
        if(item !== IDENTIFIER){
            result[entry[IDENTIFIER]][item] = (result[entry[IDENTIFIER]][item] || 0) + entry[item];
        }
    })

});

Hope you find this useful

Comments

0

Try this my friend!

var json = [{
    "STATUS":"ACTIVE",
    "AMOUNT":200,
    "pENDING":100,
},
{
    "STATUS":"NOTACTIVE",
    "AMOUNT":100,
    "pENDING":30,
},
{
    "STATUS":"NOTACTIVE",
    "AMOUNT":150,
    "pENDING":10,
}];
                
                
var result =  [];


var totalActive = 0;
var amountActive = 0;
var pendingActive = 0;

var totalNotActive = 0;
var pendingNotActive= 0;
var amountNotActive = 0;



for(var i=0; i < json.length; i++){
    var item = json[i];
    
    if (item.STATUS.toString() == "ACTIVE"){
        totalActive +=1;
        amountActive = amountActive + item.AMOUNT;
        pendingActive = pendingActive + item.pENDING;
    }
    else
    {
        totalNotActive +=1;
        amountNotActive = amountNotActive + item.AMOUNT;
        pendingNotActive = pendingNotActive + item.pENDING;
    }
                
}

result.push({ "STATUS" : "ACTIVE" ,    "COUNT" : totalActive.toString(),     "AMOUNT" : amountActive.toString(),    "pENDING" : pendingActive.toString() })
result.push({ "STATUS" : "NOTACTIVE" , "COUNT" : totalNotActive.toString(),  "AMOUNT" : amountNotActive.toString(), "pENDING" : pendingNotActive.toString() })


console.log(result);

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.