【发布时间】:2020-07-28 13:38:43
【问题描述】:
问题:我想将数据存储到具有动态变化字段的 json 文件中。
假设我想生成以下invoice.json 文件:
{
"token": "000",
"invoices":
[
{
"invoiceId": "0001-0001-0001-0001",
"amount": 45.67
},
{
"invoiceId": "0002-0002-0002-0002",
"amount": 11.03
}
]
}
理论上我认为以下命令不起作用(为了更好地理解,我对其进行了简化):
const tokenValue = "000"
let invoiceIdValue = ""
let amountValue = ""
for(let i=0; i<1, i++) {
let data = {
"token": tokenValue,
"invoiceId": invoiceIdValue[i] // <- I don't know how to call the 1st index of the field
"amount": amountValue[i]
}
}
将其写入变量然后最终写入文件的最易读的方法是什么?
注意:我将使用 cypress 的命令 cy.writeFile('invoice.json', data) 来存储迭代值。
【问题讨论】:
-
您要创建发票数组,并且令牌在预期的 json 中只有一次,因此令牌不应该在循环中
-
对不起,但这并没有回答我的问题。出于演示目的,我包含了一个固定变量。我可以使用
cy.writeFile('invoice.json', { token: '000', Invoices[0]: data} )之类的东西,但正如我所说,这不是我的问题。
标签: javascript arrays json cypress