Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this json array response from my mysql multi query

[
  [
    {
      "block": "Saraiyahat",
      "pending": 1
    },
    {
      "block": "Jama",
      "pending": 3
    },
    {
      "block": "Dumka",
      "pending": 1
    }
  ],
  [
    {
      "block": "Dumka",
      "processing": 2
    }
  ],
  [
    {
      "block": "Saraiyahat",
      "disposed": 1
    },
    {
      "block": "Jama",
      "disposed": 1
    },
    {
      "block": "Dumka",
      "disposed": 1
    }
  ]
]


And i want it to be shorted/merged like this group by "block" name using JQuery/Javascript

[
          {
            "block": "Saraiyahat",
            "pending": 1,
            "processing": 0,
            "disposed": 1
          },
          {
            "block": "Jama",
            "pending": 3,
            "processing": 0,
            "disposed": 1
          },
          {
            "block": "Dumka",
            "pending": 1,
            "processing": 2,
            "disposed": 1
          }
      ]


Please help me doing this...
Regards

What I have tried:

I have tried jquery unique, short, merge but not achieved

var block, pending = 0, processing = 0, disposed = 0;
                    for (let i = 0; i < res.length; i++) {
                        if (res[i].status == 'Pending') {
                            pending = pending + 1;
                        }

                        if (res[i].status == 'Processing') {
                            processing = processing + 1;
                        }

                        if (res[i].status == 'Disposed') {
                            disposed = disposed + 1;
                        }
                        block = res[i].block;
                    }

                    console.log({ block: block, pending: pending, processing: processing, disposed: disposed })
Posted
Updated 24-Oct-22 1:34am
v2
Comments
CHill60 24-Oct-22 6:54am    
The "What I have tried:" section is where you need to put the code you have tried. We can then help you fix it. Use the Improve question link to add that information.
Also, please remove the "asap" - this question may be important to you, but it is not important to us - we do this in our spare time. Putting "asap" into your question is likely to get it ignored
Member 15627495 24-Oct-22 7:27am    
it's a N-dimension array, you have to read the level of depth 2.
it will be at index 1 ( because index starts at 0. ).

for ( var item in arrayInput[1] ) { console.table(item); }


// to add a specification It's a JSON array

1 solution

Seems simple enough:
JavaScript
const output = [];
input.forEach(arr => arr.forEach(b => {
	const resultBlock = output.find(r => r.block === b.block);
	if (resultBlock) {
		Object.assign(resultBlock, b);
	} else {
		output.push(b);
	}
}));
Demo[^]
Array.prototype.find() - JavaScript | MDN[^]
Object.assign() - JavaScript | MDN[^]
 
Share this answer
 
Comments
GeoFinex 24-Oct-22 10:16am    
Implemented the same code on this query
[
[
{
"department": "DRDO",
"pending": 1,
"processing": 1
}
],
[
{
"department": "DRDO",
"processing": 1
},
{
"department": "IIT",
"processing": 1
}
],
[
{
"department": "DMFT",
"disposed": 1
}
]
]

But the output is not as expected

[
{
"department": "DRDO",
"pending": 1,
"processing": 1
},
{
"department": "IIT",
"processing": 1
},
{
"department": "DMFT",
"disposed": 1
}
]
Richard Deeming 24-Oct-22 10:38am    
You've changed the name of the key property from block to department. You need to update the code to match - output.find(r => r.block === b.block) becomes output.find(r => r.department === b.department).
GeoFinex 24-Oct-22 10:42am    
I already did that, check the code below

const output = [];
res.forEach(arr => arr.forEach(b => {
const resultBlock = output.find(r => r.department == b.department);
if (resultBlock) {
Object.assign(resultBlock, b);
} else {
output.push(b);
}
}));
Richard Deeming 24-Oct-22 11:59am    
The output looks fine to me: Updated demo[^]

If it's not what you're expecting, then you need to explain what you are expecting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900