【发布时间】:2021-02-03 08:40:22
【问题描述】:
希望您能帮我解决我的问题,我正在使用 jquery $.each 迭代嵌套的 json,这是我的示例代码
function getList() {
$.getJSON("src/list.json", function(data) {
$.each(data, function( key, fax ) {
const faxItem = this;
displayOnDom.append(`<div id=faxPlan-${faxItem.id} class="pricing-table flex-item ${faxItem.recommended ? 'recommended' : ''}">
<h3 class="pricing-title">${faxItem.title}</h3>
<div class="price">${faxItem.price_per_month}<sup>/ month</sup></div>
${faxItem.button ? `<p class="rounded">${faxItem.button_text}</p>` : `<p class="norounded">${faxItem.button_text}</p>`}
<div class="table-list"></div>
`);
$.each(fax.features, function( key, list ) {
$('.table-list').append(`<p>${list.title}<span>${list.list}</span></p>`)
})
})
});
}
我有一个像这样嵌套的包含多个条目的 json 文件。
[{
"id": 1,
"title": "Pay-as-you-go",
"recommended": false,
"price_per_month": "$0.00",
"button": false,
"button_text":"For flexi-fax users, top up as you need with no lock-in contracts",
"icon": true,
"features": [{
"title": "Send Credits Included",
"list": "$0.30 Excess send credit charges "
},
{
"title": "Received Credits Included",
"list": "$0.30 Excess received credit charges "
},
{
"title": "Free Fax Number",
"list": ""
}
],
"cta": {
"label": "TRY FOR FREE",
"link": "#"
}
},
我的问题是,在第二次 $.each 迭代中,所有特征数组都在第一次迭代中一起显示。它应该显示在他们自己的迭代中。
希望你们能清楚我的问题。提前致谢。
【问题讨论】:
-
你能分享你的第二次迭代代码