skip to Main Content

project using : nodejs, expressjs and database mssql 2000 with node-mssql

I have an object sample resulted from query table as follow. the length of object will vary depend on different "model code"

 
  [
  {
    poNumber: '002',
    modelCode: 'SM-A536EZWHXID',
    orderQuantity: '1',
    invoiceQuantity: null,
    price: '1000',
    totalPrice: '1000',
    receivedDate: 'Mar 26 2023  3:26AM',
    status: null,
    remarks: null
  },
  {
    poNumber: '002',
    modelCode: 'SM-A536EZWHXID',
    orderQuantity: '1',
    invoiceQuantity: null,
    price: '1000',
    totalPrice: '1000',
    receivedDate: 'Mar 26 2023  3:26AM',
    status: null,
    remarks: null
  },
  {
    poNumber: '002',
    modelCode: 'SM-A536EZOGXID',
    orderQuantity: '1',
    invoiceQuantity: null,
    price: '1000',
    totalPrice: '1000',
    receivedDate: 'Mar 26 2023  3:26AM',
    status: null,
    remarks: null
  }
  ]

with corresponding Json

  [
  {
    "poNumber": "002",
    "modelCode": "SM-A536EZWHXID",
    "orderQuantity": "1",
    "invoiceQuantity": null,
    "price": "1000",
    "totalPrice": "1000",
    "receivedDate": "Mar 26 2023  3:26AM",
    "status": null,
    "remarks": null
  },
  {
    "poNumber": "002",
    "modelCode": "SM-A536EZWHXID",
    "orderQuantity": "1",
    "invoiceQuantity": null,
    "price": "1000",
    "totalPrice": "1000",
    "receivedDate": "Mar 26 2023  3:26AM",
    "status": null,
    "remarks": null
  },
  {
    "poNumber": "002",
    "modelCode": "SM-A536EZOGXID",
    "orderQuantity": "1",
    "invoiceQuantity": null,
    "price": "1000",
    "totalPrice": "1000",
    "receivedDate": "Mar 26 2023  3:26AM",
    "status": null,
    "remarks": null
  }
  ]

But the client need Json in this format

  {
    "poNumber": "002",
    "items : [
  {
    "modelCode": "SM-A536EZWHXID",
    "orderQuantity": "1",
    "invoiceQuantity": null,
    "price": "1000",
    "totalPrice": "1000",
    "receivedDate": "Mar 26 2023  3:26AM",
  },
  {
    "modelCode": "SM-A536EZWHXID",
    "orderQuantity": "1",
    "invoiceQuantity": null,
    "price": "1000",
    "totalPrice": "1000",
    "receivedDate": "Mar 26 2023  3:26AM",
  },
  {
    "modelCode": "SM-A536EZOGXID",
    "orderQuantity": "1",
    "invoiceQuantity": null,
    "price": "1000",
    "totalPrice": "1000",
    "receivedDate": "Mar 26 2023  3:26AM",
  }
  ],
    "status": null,
    "remarks": null
  }

it is look alike grouping into :

header part : "poNumber"
body part : with label "items" and its item "modelCode", "orderQuantity", "invoiceQuantity, "price", "totalPrice", receivedDate"
footer part : "status" and "remarks"

How can i generate object from table or modify json to get json structure as needed like that?

modify json and manipulate query result but did not working

3

Answers


  1. Chosen as BEST ANSWER

    const data =  [{"poNumber": "002","modelCode": "SM-A536EZWHXID","orderQuantity": "1","invoiceQuantity": null,"price": "1000","totalPrice": "1000","receivedDate": "Mar 26 2023  3:26AM","status": null,"remarks": null},{"poNumber": "002","modelCode": "SM-A536EZWHXID","orderQuantity": "1","invoiceQuantity": null,"price": "1000","totalPrice": "1000","receivedDate": "Mar 26 2023  3:26AM","status": null,"remarks": null},{"poNumber": "002","modelCode": "SM-A536EZOGXID","orderQuantity": "1","invoiceQuantity": null,"price": "1000","totalPrice": "1000","receivedDate": "Mar 26 2023  3:26AM","status": null,"remarks": null}];
    
    const map = new Map(data.map(({poNumber, status, remarks}) => 
        [poNumber, {poNumber, items: [], status, remarks}]
    ));
    for (const {poNumber, status, remarks, ...rest} of data) {
        map.get(poNumber).items.push(rest);
    }
    const result = [...map.values()];
    console.log(result);

    @trincot thank you for your response

    I try it in my vscode, but why the result is like this?

    [
      {
        poNumber: '002',
        items: [ [Object], [Object], [Object] ],
        status: null,
        remarks: null
      }
    ]
    

    ** Edited/Updated** Result above from standalone execution

    But when i integrated into my code, result is just perfect as desired

    Case Resolved


  2. Here is a way to group by:

    • Create a Map keyed by poNumber and with as corresponding value the target object structure with items an empty array and status and remarks properties.
    • Iterate the data to populate the items array for the entry retrieved from that map by poNumber
    • Extract the values from the Map.
    const data =  [{"poNumber": "002","modelCode": "SM-A536EZWHXID","orderQuantity": "1","invoiceQuantity": null,"price": "1000","totalPrice": "1000","receivedDate": "Mar 26 2023  3:26AM","status": null,"remarks": null},{"poNumber": "002","modelCode": "SM-A536EZWHXID","orderQuantity": "1","invoiceQuantity": null,"price": "1000","totalPrice": "1000","receivedDate": "Mar 26 2023  3:26AM","status": null,"remarks": null},{"poNumber": "002","modelCode": "SM-A536EZOGXID","orderQuantity": "1","invoiceQuantity": null,"price": "1000","totalPrice": "1000","receivedDate": "Mar 26 2023  3:26AM","status": null,"remarks": null}];
    
    const map = new Map(data.map(({poNumber, status, remarks}) => 
        [poNumber, {poNumber, items: [], status, remarks}]
    ));
    for (const {poNumber, status, remarks, ...rest} of data) {
        map.get(poNumber).items.push(rest);
    }
    const result = [...map.values()];
    console.log(result);
    Login or Signup to reply.
  3. @vimmuth, thank you for your response

    this is result in my vscode

        {
      "002": {
        "poNumber": "002",
        "items": [
          {
            "modelCode": "SM-A536EZWHXID",
            "orderQuantity": "1",
            "invoiceQuantity": null,
            "price": "1000",
            "totalPrice": "1000",
            "receivedDate": "Mar 26 2023  3:26AM"
          },
          {
            "modelCode": "SM-A536EZWHXID",
            "orderQuantity": "1",
            "invoiceQuantity": null,
            "price": "1000",
            "totalPrice": "1000",
            "receivedDate": "Mar 26 2023  3:26AM"
          {
            "modelCode": "SM-A536EZOGXID",
            "orderQuantity": "1",
            "invoiceQuantity": null,
            "price": "1000",
            "totalPrice": "1000",
            "receivedDate": "Mar 26 2023  3:26AM"
          }
        ],
        "status": null,
        "remarks": null
      }
    }
    

    How to remove excessive line number #2?

    "002": {

    thank you

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search