如何自定义/修改对象查询结果的JSON响应?

5q4ezhmt  于 2023-03-31  发布在  其他
关注(0)|答案(3)|浏览(104)

项目使用: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并操作查询结果,但不起作用

jq6vz3qz

jq6vz3qz1#

下面是一种分组方式:

  • 创建一个Map,其键为poNumber,对应的值为目标对象结构,其中items为空数组,statusremarks为属性。
  • 迭代数据以填充poNumber从Map中检索到的条目的items数组
  • 从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);
vyu0f0g1

vyu0f0g12#

@vimmuth,谢谢你的回应
这是我的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
  }
}

如何删除多余的行号#2?
“002”:{
谢谢

vlurs2pr

vlurs2pr3#

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感谢您的回复
我在我的vscode中尝试了一下,但是为什么结果是这样的呢?

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

已编辑/更新以上结果来自独立执行

但是当我集成到我的代码中时,result就像我期望的那样完美
已解决的病例

相关问题