项目使用:nodejs,expressjs和数据库mssql 2000与node-mssql
我有一个对象的例子,从查询表如下。对象的长度会有所不同,根据不同的“模型代码”
[
{
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
}
]
对应的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
}
]
但是客户端需要这种格式的Json
{
"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
}
它看起来类似于分组为:
标题部分:“poNumber”正文部分:带有标签“项目”及其项目“型号代码”、“订单数量”、“发票数量”、“价格”、“总价”、"接收日期”的页脚部分:“地位”和“备注”
如何从表中生成对象或修改JSON以获得需要的JSON结构?
修改json并操作查询结果,但不起作用
3条答案
按热度按时间jq6vz3qz1#
下面是一种分组方式:
poNumber
,对应的值为目标对象结构,其中items
为空数组,status
和remarks
为属性。poNumber
从Map中检索到的条目的items
数组vyu0f0g12#
@vimmuth,谢谢你的回应
这是我的vscode的结果
如何删除多余的行号#2?
“002”:{
谢谢
vlurs2pr3#
@Trincot感谢您的回复
我在我的vscode中尝试了一下,但是为什么结果是这样的呢?
已编辑/更新以上结果来自独立执行
但是当我集成到我的代码中时,result就像我期望的那样完美
已解决的病例