英文:
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代码的描述,包括使用 reduce
和 Object.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) => {
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);
<!-- 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=[{"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);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论