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
@trincot thank you for your response
I try it in my vscode, but why the result is like this?
** Edited/Updated** Result above from standalone execution
But when i integrated into my code, result is just perfect as desired
Case Resolved
Here is a way to group by:
poNumber
and with as corresponding value the target object structure withitems
an empty array andstatus
andremarks
properties.items
array for the entry retrieved from that map bypoNumber
@vimmuth, thank you for your response
this is result in my vscode
How to remove excessive line number #2?
"002": {
thank you