下面是JSX格式的代码。r.invoice_details
的输出是这样的
{"rate":1522,"quantity":1,"discount":null,"itemName":"Gold Members club (offline)","amount":1522,"noEdit":true}
它是字符串的形式。我想要显示的只是字符串中的itemName。内联函数最好的写法是什么?
{
id: "itemName",
label: "Item",
customCell: (r, i) => {
return <td key={`${i}-itemName`}>
{r.invoice_details}
</td>;
}
},
您可以使用JSON.parse()
const json = '{"rate":1522,"quantity":1,"discount":null,"itemName":"Gold Members club (offline)","amount":1522,"noEdit":true}';
const obj = JSON.parse(json);
console.log(obj.itemName);
那么你的结束代码看起来就像这样,
{
id: "itemName",
label: "Item",
customCell: (r, i) => {
return <td key={`${i}-itemName`}>
{JSON.parse(r.invoice_details).itemName}
</td>;
}
},
可以使用JSON.parse()函数:
{JSON.parse(r.invoice_details).itemName}
{
id: "itemName",
label: "Item",
customCell: (r, i) => {
return <td key={`${i}-itemName`}>
{JSON.parse(r.invoice_details).itemName}
</td>;
}
},
尝试对象。JSON.parse()
let json = JSON.parse(r.invoice_details)
let inv = Object.keys(json)
let inv2 = Object.values(json)
let Id = inv2[inv1.indexOf("itemName")]