【发布时间】:2019-07-12 18:24:54
【问题描述】:
我在后端使用KOA 调用来自localhost:3000 的json 数据,然后我需要稍后将这些数据放入滑块但排序到数组中。所有这些都在一个自定义的 WordPress 主题中。我正在努力完成这项工作,因为我是新手。任何人都可以帮助查看我的代码,看看我是否可以在现有代码中转换为数组,以便在将数据放入预期文档之前对数据进行排序或排序?我打电话给main.js,在那里我保留了调用和格式化数据的函数。在这里,我将包含main.js 内容以及json 文件。提前谢谢你。
insurer.json
{
"insurer1": {
"basic": {
"title": "3rd Party basic",
"price_from": "520"
},
"plus": {
"title": "Ample 3rd Party",
"price_from": "612"
},
"full": {
"title": "High Risk",
"price_from": "723"
}
},
"insurer2": {
"basic": {
"title": "3rd Party basic",
"price_from": "425"
},
"plus": {
"title": "Ample 3rd Party",
"price_from": "580"
},
"full": {
"title": "High Risk",
"price_from": "780"
}
},
"insurer3": {
"basic": {
"title": "3rd Party basic",
"price_from": "450"
},
"plus": {
"title": "Ample 3rd Party",
"price_from": "543"
},
"full": {
"title": "High Risk",
"price_from": "733"
}
}
}
main.js
const insuranceUrl = 'http://localhost:3000/insurances';
const insuranceContainer = document.querySelector('.insurance');
let Euro = '€/mes';
let euro_symbol = '€';
fetch(insuranceUrl)
.then(response => response.json())
.then(data => {
var sectionx=document.createElement("section");
sectionx.classList.add("custom_section");
var insurer1=sectionx.cloneNode(true);
insurer1.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer1']['basic']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer1']['basic']['price_from']+' '+Euro+'</label>'
+'</div>';
insuranceContainer.appendChild(insurer1);
var insurer1=sectionx.cloneNode(true);
insurer1.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer1']['plus']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer1']['plus']['price_from']+' '+Euro+'</label>';
insuranceContainer.appendChild(insurer1);
var insurer1=sectionx.cloneNode(true);
insurer1.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer3']['full']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer1']['full']['price_from']+' '+Euro+'</label>';
insuranceContainer.appendChild(insurer1);
var insurer2=sectionx.cloneNode(true);
insurer2.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer2']['basic']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer2']['basic']['price_from']+' '+Euro+'</label>'
+'</div>';
insuranceContainer.appendChild(insurer2);
var insurer2=sectionx.cloneNode(true);
insurer2.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer2']['plus']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer2']['plus']['price_from']+' '+Euro+'</label>'
+'</div>';
insuranceContainer.appendChild(insurer2);
var insurer2=sectionx.cloneNode(true);
insurer2.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer2']['full']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer2']['full']['price_from']+' '+Euro+'</label>'
+'</div>';
insuranceContainer.appendChild(insurer2);
var insurer3=sectionx.cloneNode(true);
insurer3.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer3']['basic']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer3']['basic']['price_from']+' '+Euro+'</label>'
+'</div>';
insuranceContainer.appendChild(insurer3);
var insurer3=sectionx.cloneNode(true);
insurer3.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer3']['plus']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer3']['plus']['price_from']+' '+Euro+'</label>'
+'</div>';
insuranceContainer.appendChild(insurer3);
var insurer3=sectionx.cloneNode(true);
insurer3.innerHTML='<div style="border:1px solid #c4c4c4; padding: 15px; min-width: 174px; text-align: left;">'
+'<label style="color:#27accd; font-weight:600; line-height: 20px;">'+data['insurer3']['full']['title']+'</label>'
+'<br />'
+'<label style="color:gray; line-height: 20px;">desde</label>'
+'<br />'
+'<label style="font-weight: bold; line-height: 20px;">'+data['insurer3']['full']['price_from']+' '+Euro+'</label>'
+'</div>';
insuranceContainer.appendChild(insurer3);
});
【问题讨论】:
-
您希望阵列是什么样的?您是否希望 JSON 文件中的每个保险公司对象成为数组中自己的元素?保险公司的每个孩子?
-
insurer1 基本款,最低价格,insurer2 基本款,中等价格,insurer3 基本款,最高价格的insurer1 plus,最低价格,insurer2 plus,中等价格,insurer3 plus,最高价格insurer1 完整,最低价格,insurer3 完整,中价,insurer3,全价,最高价,但我不能操作原始文件中的 json 数据,因为公司会不时更改它。
-
也就是说,先按基本分组,再按全分组。由于价格已经在 JSON 中排序,我不需要对它们进行排序。
标签: javascript json wordpress koa