我需要将我的Json数据整理成可迭代的形式。

huangapple go评论69阅读模式
英文:

I need to Arrange my Json data to iteratable one

问题

[
{"TenantId": 96},
{"BatchNo": "B-638248651272667323PI345-1"}
]

英文:
[{"TenantId":96,"BatchNo":"B-638248651272667323PI345-1","Rate":10.00,"Qty":1.00,"Discount":0.00,"Gross":10.00,"Net":10.00,"TransactionType":1,"CombinationId":null,"PurchaseInvoiceDetailsId":11394,"PurchaseInvoiceDetails":null,"SalesInvoiceDetailId":null,"SalesInvoiceDetail":null,"SalesReturnDetailsId":null,"SalesReturnDetails":null,"PurchaseReturnDetailsId":null,"PurchaseReturnDetails":null,"ProductProfileId":6490,"ProductProfile":null,"ProductVarientLinksId":null,"ProductVarientLinks":null,"ProductionOrderDetailId":null,"ProductionOrderDetail":null,"QuickSaleInvoiceDetailId":null,"QuickSaleInvoiceDetail":null,"DirectSaleDetailId":null,"DirectSaleDetail":null,"QuickPreparedItemId":null,"QuickPreparedItem":null,"QuickSaleFoodWastgeId":null,"QuickSaleFoodWastge":null,"TransferDetailId":null,"TransferDetail":null,"StoreId":245,"Store":null,"BranchId":233,"Branch":null,"UnitId":238,"Unit":null,"BaseUnitId":239,"BaseUnit":null,"BaseQty":1000.00,"DamageStockDetailsId":null,"DamageStockDetails":null,"StockUpdateDetailId":null,"StockUpdateDetail":null,"QuickSaleOrderDetailId":null,"QuickRaweMaterialsUsedId":null,"QuickRaweMaterialsUsed":null,"Date":"2023-07-13T00:00:00","BarcodeNumber":null,"CompanyId":66,"Company":null,"RawmaterialsReturnId":null,"RawmaterialsReturn":null,"FinishedGoodDetailId":null,"FinishedGoodDetail":null,"DeliveryDetailsId":null,"DeliveryDetails":null,"GrnDetailsId":null,"GrnDetails":null,"QRCodeDetailsId":null,"QRCodeDetails":null,"UPICode":null,"MaxRate":0.00,"SalesRate":0.00,"ExpiryDate":null,"SerialNumber1":null,"SerialNumber2":null,"ChasisNo":null,"MotorNo":null,"RepackingId":null,"Repacking":null,"RepackingDetailsId":null,"RepackingDetails":null,"StockCorrectionDetailsId":null,"StockCorrectionDetails":null,"PurchaseRate":0.00,"FinancialYearMastersId":312,"FinancialYearMasters":null,"Reference":null,"IsDeleted":false,"DeleterUserId":null,"DeletionTime":null,"LastModificationTime":null,"LastModifierUserId":null,"CreationTime":"2023-07-13T17:27:14.3584215","CreatorUserId":253,"Id":47819}]

This is my Json data and this varying the key values so i need to loop with the below format
eg: [ {"TenantId":96},{"BatchNo":"B-638248651272667323PI345-1"} ]

答案1

得分: 0

以下是您要翻译的内容:

"Reduce! 我认为这是您所要求的,它将对象拆分为单独的键/值对。如果您希望消除嵌套数组,可以使用 .flat()。"

function transformArray(array) {
  return array.reduce((transformedArray, item) => {
    const transformedItem = Object.entries(item).reduce((acc, [key, value]) => {
      acc.push({ [key]: value });
      return acc;
    }, []);
    transformedArray.push(transformedItem);
    return transformedArray;
  }, []);
}

const originalArray = [
  { "foo": "1", "bar": "2", "batch": "1235", "price": "10.00" },
  { "foo": "3", "bar": "4" }
];

const transformedArray = transformArray(originalArray);
console.log(transformedArray);

请注意,这段代码是对某种JavaScript代码的描述,包括使用 reduceObject.entries 来转换数组。

英文:

Reduce! I think this is what you are asking for, it takes the object and splits it into individual key/values. You can use .flat() to get to get rid of the nested arrays if you want.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function transformArray(array) {
  return array.reduce((transformedArray, item) =&gt; {
    const transformedItem = Object.entries(item).reduce((acc, [key, value]) =&gt; {
      acc.push({ [key]: value });
      return acc;
    }, []);
    transformedArray.push(transformedItem);
    return transformedArray;
  }, []);
}

const originalArray = [
  { &quot;foo&quot;: &quot;1&quot;, &quot;bar&quot;: &quot;2&quot;, &quot;batch&quot; : &quot;1235&quot;, &quot;price&quot;: &quot;10.00&quot; },
  { &quot;foo&quot;: &quot;3&quot;, &quot;bar&quot;: &quot;4&quot; }
];

const transformedArray = transformArray(originalArray);
console.log(transformedArray);

<!-- end snippet -->

答案2

得分: 0

您可以对每个对象的条目进行map操作,以从每个键值对创建一个对象。

const data = [{"TenantId": 96, "BatchNo": "B-638248651272667323PI345-1", "Rate": 10.00, "Qty": 1.00, "Discount": 0.00, "Gross": 10.00, "Net": 10.00, "TransactionType": 1, "CombinationId": null, "PurchaseInvoiceDetailsId": 11394, "PurchaseInvoiceDetails": null, "SalesInvoiceDetailId": null, "SalesInvoiceDetail": null, "SalesReturnDetailsId": null, "SalesReturnDetails": null, "PurchaseReturnDetailsId": null, "PurchaseReturnDetails": null, "ProductProfileId": 6490, "ProductProfile": null, "ProductVarientLinksId": null, "ProductVarientLinks": null, "ProductionOrderDetailId": null, "ProductionOrderDetail": null, "QuickSaleInvoiceDetailId": null, "QuickSaleInvoiceDetail": null, "DirectSaleDetailId": null, "DirectSaleDetail": null, "QuickPreparedItemId": null, "QuickPreparedItem": null, "QuickSaleFoodWastgeId": null, "QuickSaleFoodWastge": null, "TransferDetailId": null, "TransferDetail": null, "StoreId": 245, "Store": null, "BranchId": 233, "Branch": null, "UnitId": 238, "Unit": null, "BaseUnitId": 239, "BaseUnit": null, "BaseQty": 1000.00, "DamageStockDetailsId": null, "DamageStockDetails": null, "StockUpdateDetailId": null, "StockUpdateDetail": null, "QuickSaleOrderDetailId": null, "QuickRaweMaterialsUsedId": null, "QuickRaweMaterialsUsed": null, "Date": "2023-07-13T00:00:00", "BarcodeNumber": null, "CompanyId": 66, "Company": null, "RawmaterialsReturnId": null, "RawmaterialsReturn": null, "FinishedGoodDetailId": null, "FinishedGoodDetail": null, "DeliveryDetailsId": null, "DeliveryDetails": null, "GrnDetailsId": null, "GrnDetails": null, "QRCodeDetailsId": null, "QRCodeDetails": null, "UPICode": null, "MaxRate": 0.00, "SalesRate": 0.00, "ExpiryDate": null, "SerialNumber1": null, "SerialNumber2": null, "ChasisNo": null, "MotorNo": null, "RepackingId": null, "Repacking": null, "RepackingDetailsId": null, "RepackingDetails": null, "StockCorrectionDetailsId": null, "StockCorrectionDetails": null, "PurchaseRate": 0.00, "FinancialYearMastersId": 312, "FinancialYearMasters": null, "Reference": null, "IsDeleted": false, "DeleterUserId": null, "DeletionTime": null, "LastModificationTime": null, "LastModifierUserId": null, "CreationTime": "2023-07-13T17:27:14.3584215", "CreatorUserId": 253, "Id": 47819}];
const res = data.flatMap(o => Object.entries(o).map(([k, v]) => ({[k]: v})));
console.log(res);

这段代码可以将每个对象的键值对映射为一个对象数组。

英文:

You can map over the entries of each object to create an object from each key-value pair.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data=[{&quot;TenantId&quot;:96,&quot;BatchNo&quot;:&quot;B-638248651272667323PI345-1&quot;,&quot;Rate&quot;:10.00,&quot;Qty&quot;:1.00,&quot;Discount&quot;:0.00,&quot;Gross&quot;:10.00,&quot;Net&quot;:10.00,&quot;TransactionType&quot;:1,&quot;CombinationId&quot;:null,&quot;PurchaseInvoiceDetailsId&quot;:11394,&quot;PurchaseInvoiceDetails&quot;:null,&quot;SalesInvoiceDetailId&quot;:null,&quot;SalesInvoiceDetail&quot;:null,&quot;SalesReturnDetailsId&quot;:null,&quot;SalesReturnDetails&quot;:null,&quot;PurchaseReturnDetailsId&quot;:null,&quot;PurchaseReturnDetails&quot;:null,&quot;ProductProfileId&quot;:6490,&quot;ProductProfile&quot;:null,&quot;ProductVarientLinksId&quot;:null,&quot;ProductVarientLinks&quot;:null,&quot;ProductionOrderDetailId&quot;:null,&quot;ProductionOrderDetail&quot;:null,&quot;QuickSaleInvoiceDetailId&quot;:null,&quot;QuickSaleInvoiceDetail&quot;:null,&quot;DirectSaleDetailId&quot;:null,&quot;DirectSaleDetail&quot;:null,&quot;QuickPreparedItemId&quot;:null,&quot;QuickPreparedItem&quot;:null,&quot;QuickSaleFoodWastgeId&quot;:null,&quot;QuickSaleFoodWastge&quot;:null,&quot;TransferDetailId&quot;:null,&quot;TransferDetail&quot;:null,&quot;StoreId&quot;:245,&quot;Store&quot;:null,&quot;BranchId&quot;:233,&quot;Branch&quot;:null,&quot;UnitId&quot;:238,&quot;Unit&quot;:null,&quot;BaseUnitId&quot;:239,&quot;BaseUnit&quot;:null,&quot;BaseQty&quot;:1000.00,&quot;DamageStockDetailsId&quot;:null,&quot;DamageStockDetails&quot;:null,&quot;StockUpdateDetailId&quot;:null,&quot;StockUpdateDetail&quot;:null,&quot;QuickSaleOrderDetailId&quot;:null,&quot;QuickRaweMaterialsUsedId&quot;:null,&quot;QuickRaweMaterialsUsed&quot;:null,&quot;Date&quot;:&quot;2023-07-13T00:00:00&quot;,&quot;BarcodeNumber&quot;:null,&quot;CompanyId&quot;:66,&quot;Company&quot;:null,&quot;RawmaterialsReturnId&quot;:null,&quot;RawmaterialsReturn&quot;:null,&quot;FinishedGoodDetailId&quot;:null,&quot;FinishedGoodDetail&quot;:null,&quot;DeliveryDetailsId&quot;:null,&quot;DeliveryDetails&quot;:null,&quot;GrnDetailsId&quot;:null,&quot;GrnDetails&quot;:null,&quot;QRCodeDetailsId&quot;:null,&quot;QRCodeDetails&quot;:null,&quot;UPICode&quot;:null,&quot;MaxRate&quot;:0.00,&quot;SalesRate&quot;:0.00,&quot;ExpiryDate&quot;:null,&quot;SerialNumber1&quot;:null,&quot;SerialNumber2&quot;:null,&quot;ChasisNo&quot;:null,&quot;MotorNo&quot;:null,&quot;RepackingId&quot;:null,&quot;Repacking&quot;:null,&quot;RepackingDetailsId&quot;:null,&quot;RepackingDetails&quot;:null,&quot;StockCorrectionDetailsId&quot;:null,&quot;StockCorrectionDetails&quot;:null,&quot;PurchaseRate&quot;:0.00,&quot;FinancialYearMastersId&quot;:312,&quot;FinancialYearMasters&quot;:null,&quot;Reference&quot;:null,&quot;IsDeleted&quot;:false,&quot;DeleterUserId&quot;:null,&quot;DeletionTime&quot;:null,&quot;LastModificationTime&quot;:null,&quot;LastModifierUserId&quot;:null,&quot;CreationTime&quot;:&quot;2023-07-13T17:27:14.3584215&quot;,&quot;CreatorUserId&quot;:253,&quot;Id&quot;:47819}];
const res = data.flatMap(o =&gt; Object.entries(o).map(([k, v]) =&gt; ({[k]:v})));
console.log(res);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月13日 20:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定