{
id: 8,
customerName: "xyz",
customerMobileNumber: "123456789",
customerBillingAddress: "xyz address",
customerShippingAddress: "xyz address",
customerProductPurchasedDate: "2021-11-09T09:07:00.000Z",
customerGstNumber: "xyz",
customerHsnNumber: "xyz",
addedInvoiceProductDetails: "[{"productquantity":"5","productprice":"5","productgst":"5","productname":"xyz","producttotalprice":"26.25","id":"2021-11-13T09:08:20.071Z"}]",
created_at: "2021-11-13T09:08:25.000000Z",
updated_at: "2021-11-13T09:08:25.000000Z"
},
我在我的一個專案 API[ http://127.0.0.1:8000/api/invoice_details/8 ] 中獲得了高于值,我面臨的問題是我無法映射引數,即addedInvoiceProductDetails我得到的型別對于引數addedInvoiceProductDetails是字串。所以地圖不適用于字串。因此,我如何將其轉換為物件并映射它。
注意: 我從 API 收到了對addedInvoiceProductDetails引數的回應如下
[{"productquantity":"1","productprice":"1000","productgst":"18","productname":"Street Light","producttotalprice":"1180","id":"2021-11-18T12:11:31.137Z"},{"productname":"Solar","productquantity":"2","productprice":"50","productgst":"10","producttotalprice":"110","id":"2021-11-18T12:11:43.935Z"}]
我從 API 得到的回應本身就被決議了,但是當我檢查它的 typeof 時,它仍然是作為字串給出的。
如果我提供了解決方案,那將非常有幫助,我過去 2 天一直在解決這個問題。
uj5u.com熱心網友回復:
您需要對值進行 JSON.parse
const response = [{
id: 8,
customerName: "xyz",
customerMobileNumber: "123456789",
customerBillingAddress: "xyz address",
customerShippingAddress: "xyz address",
customerProductPurchasedDate: "2021-11-09T09:07:00.000Z",
customerGstNumber: "xyz",
customerHsnNumber: "xyz",
addedInvoiceProductDetails: "[{\"productquantity\":\"5\",\"productprice\":\"5\",\"productgst\":\"5\",\"productname\":\"xyz\",\"producttotalprice\":\"26.25\",\"id\":\"2021-11-13T09:08:20.071Z\"}]",
created_at: "2021-11-13T09:08:25.000000Z",
updated_at: "2021-11-13T09:08:25.000000Z"
},
{
id: 10,
customerName: "xyz",
customerMobileNumber: "123456789",
customerBillingAddress: "xyz",
customerShippingAddress: "xyz",
customerProductPurchasedDate: "2021-11-11T09:26:00.000Z",
customerGstNumber: "xyz",
customerHsnNumber: "xyz",
addedInvoiceProductDetails: "[{\"productquantity\":\"5\",\"productprice\":\"5\",\"productgst\":\"5\",\"productname\":\"gggg\",\"producttotalprice\":\"26.25\",\"id\":\"2021-11-13T09:27:17.639Z\"}]",
created_at: "2021-11-13T09:27:20.000000Z",
updated_at: "2021-11-13T09:27:20.000000Z"
},
]
response.forEach(({addedInvoiceProductDetails})=> console.log(JSON.parse(addedInvoiceProductDetails)))
uj5u.com熱心網友回復:
您需要回應JSON.parse的addedInvoiceProductDetails屬性。前任:
let response = {
id: 8,
customerName: "xyz",
customerMobileNumber: "123456789",
customerBillingAddress: "xyz address",
customerShippingAddress: "xyz address",
customerProductPurchasedDate: "2021-11-09T09:07:00.000Z",
customerGstNumber: "xyz",
customerHsnNumber: "xyz",
addedInvoiceProductDetails: "[{\"productquantity\":\"5\",\"productprice\":\"5\",\"productgst\":\"5\",\"productname\":\"xyz\",\"producttotalprice\":\"26.25\",\"id\":\"2021-11-13T09:08:20.071Z\"}]",
created_at: "2021-11-13T09:08:25.000000Z",
updated_at: "2021-11-13T09:08:25.000000Z"
}
// without JSON.parse the value of addedInvoiceProductDetails is a string
console.log(typeof response.addedInvoiceProductDetails)
// string
// with JSON.parse the value of addedInvoiceProductDetails is a object
console.log(typeof JSON.parse(response.addedInvoiceProductDetails))
// object
// if you parse and then log you'll see it's an object with accessable properties
console.log(JSON.parse(response.addedInvoiceProductDetails))
/* [ { productquantity: '5',
productprice: '5',
productgst: '5',
productname: 'xyz',
producttotalprice: '26.25',
id: '2021-11-13T09:08:20.071Z' } ] */
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360027.html
標籤:javascript html css mysql 反应
上一篇:在塊中集成欄位
