0

I am getting results from my MySQL in an array like this:

    [ RowDataPacket {
        Shipper_Name: 'SONA CEREAL PRODUCTS PRIVATE LIMITED',
        Job_ID: 6,
        ShipperInvoiceNo: '12',
        Packing: '4X10Kgs',
        TotalBags: 4000,
        receivedbags: null,
        Diff: null },
      RowDataPacket {
        Shipper_Name: 'AGROW ALLIED VENTURES P LTD',
        Job_ID: 7,
        ShipperInvoiceNo: '0129-20',
        Packing: '1X30Kgs',
        TotalBags: 4800,
        receivedbags: 3670,
        Diff: -1130 },
      RowDataPacket {
        Shipper_Name: 'ATLANTIC CROP SCIENCE P LTD',
        Job_ID: 8,
        ShipperInvoiceNo: '0334/20-21',
        Packing: '8X5Kgs',
        TotalBags: 8758,
        receivedbags: 8758,
        Diff: 0 },
      RowDataPacket {
        Shipper_Name: 'ATLANTIC CROP SCIENCE P LTD',
        Job_ID: 8,
        ShipperInvoiceNo: '0334/20-21',
        Packing: '1X40Kgs',
        TotalBags: 1800,
        receivedbags: 1800,
        Diff: 0 },
      RowDataPacket {
        Shipper_Name: 'ATLANTIC CROP SCIENCE P LTD',
        Job_ID: 8,
        ShipperInvoiceNo: '0334/20-21',
        Packing: '4X10Kgs',
        TotalBags: 4130,
        receivedbags: 530,
        Diff: -3600 } ]

Now I want to convert this JSON data or array to the array below as I want to fill a html table with this data.

    [{"Shipper_Name":"SONA CEREAL PRODUCTS PRIVATE LIMITED"},{"Job_ID":"6","ShipperInvoiceNo": "12","Packing": "4X10Kgs","TotalBags": "4000","receivedbags": "null","Diff": "null"},{"Shipper_Name": "AGROW ALLIED VENTURES P LTD"},{"Job_ID": "7","ShipperInvoiceNo": "0129-20","Packing": "1X30Kgs","TotalBags": "4800","receivedbags": "3670","Diff": "-1130" },{"Shipper_Name": "ATLANTIC CROP SCIENCE P LTD"},{"Job_ID": "8","ShipperInvoiceNo": "0334/20-21","Packing": "8X5Kgs","TotalBags": "8758","receivedbags": "8758","Diff": "0"},{"Job_ID": "8","ShipperInvoiceNo": "0334/20-21","Packing": "1X40Kgs","TotalBags": "1800","receivedbags": "1800","Diff": "0"},{"Job_ID": "8","ShipperInvoiceNo": "0334/20-21","Packing": "4X10Kgs","TotalBags": "4130","receivedbags": "530", "Diff": "-3600" }]
1
  • Could you double-check and confirm whether the result array format is the one you posted? I would expect something like [{ RowDataPacket: { ... } }, { RowDataPacket: { ... } }, ...] rather than [RowDataPacket { ... }, RowDataPacket { ... }, ...]. Commented Dec 3, 2020 at 9:38

1 Answer 1

1

Assuming you have setup the input as

let input = [{ Shipper_Name: ... }, {...}, ...];

Then the following code would do the trick

let output = [];
let shipperName = null;
for (let ii = 0; ii < input.length; ii++) {
  let val = input[ii];
  if (val["Shipper_Name"] !== shipperName) {
    shipperName = val["Shipper_Name"];
    output.push({Shipper_Name: shipperName});
  }
  let copy = JSON.parse(JSON.stringify(val));
  delete copy["Shipper_Name"];
  output.push(copy);
}

console.log(output);
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.