我正在尝试在 Vuejs
上构建一个小型应用程序,其中我有一组数组,这些数组通过 api 响应出现,该响应提供以下输出:
{
"data":
{
"Real Estate Regulatory Act":[
{
"id":603,
"project_id":2392,
"parent_type":"Real Estate Regulatory Act",
"type":"Building Plan Approval",
"name":"FORMS.pdf",
"link":"https://.....DyumatHotelsFORMS.pdf"
}
],
"Environmental Clearance":[
{
"id":602,
"project_id":2392,
"parent_type":"Environmental Clearance",
"type":"Form 1",
"name":"HotelsCPEMP.pdf",
"link":"https://.....MarineandDyumatHotelsCPEMP.pdf"
}
],
"Document":[
{
"id":601,
"project_id":2392,
"parent_type":"Document",
"type":"Land Details",
"name":"FORMS.pdf",
"link":"https://....HotelsFORMS.pdf"
}
],
"Miscellaneous Approvals":[
{
"id":604,
"project_id":2392,
"parent_type":
"Miscellaneous Approvals",
"type":"Not Reported",
"name":"Report Part 1.pdf",
"link":"https://...Report Part 1.pdf"
}
]
}
}
我想根据以下数组的索引对此 JavaScript 进行排序:
['Document', 'Environmental Clearance', 'Real Estate Regulatory Act', 'Miscellaneous Approvals']
所以我的最终结果将是:
{
"data":
{
"Document":[
{
"id":601,
"project_id":2392,
"parent_type":"Document",
"type":"Land Details",
"name":"FORMS.pdf",
"link":"https://....HotelsFORMS.pdf"
}
],
"Environmental Clearance":[
{
"id":602,
"project_id":2392,
"parent_type":"Environmental Clearance",
"type":"Form 1",
"name":"HotelsCPEMP.pdf",
"link":"https://.....MarineandDyumatHotelsCPEMP.pdf"
}
],
"Real Estate Regulatory Act":[
{
"id":603,
"project_id":2392,
"parent_type":"Real Estate Regulatory Act",
"type":"Building Plan Approval",
"name":"FORMS.pdf",
"link":"https://.....DyumatHotelsFORMS.pdf"
}
],
"Miscellaneous Approvals":[
{
"id":604,
"project_id":2392,
"parent_type":
"Miscellaneous Approvals",
"type":"Not Reported",
"name":"Report Part 1.pdf",
"link":"https://...Report Part 1.pdf"
}
]
}
}
我的代码目前如下所示:
if(response.status === 200)
{
let docs = response.data.data;
let sortDocs = ['Document', 'Environmental Clearance', 'Real Estate Regulatory Act', 'Miscellaneous Approvals'];
let result = []
sortDocs.forEach(function(key) {
this.subscProDocument[key] = result.push(docs[key])
})
}
我收到这样的错误:
Uncaught (in promise) TypeError: Cannot read property 'subscProDocument' of undefined
我已经在 data() 中定义了这个 subscProDocument
并初始化为空数组。帮我解决这个问题。谢谢
最佳答案
let data = {
"data":
{
"Real Estate Regulatory Act":[
{
"id":603,
"project_id":2392,
"parent_type":"Real Estate Regulatory Act",
"type":"Building Plan Approval",
"name":"FORMS.pdf",
"link":"https://.....DyumatHotelsFORMS.pdf"
}
],
"Environmental Clearance":[
{
"id":602,
"project_id":2392,
"parent_type":"Environmental Clearance",
"type":"Form 1",
"name":"HotelsCPEMP.pdf",
"link":"https://.....MarineandDyumatHotelsCPEMP.pdf"
}
],
"Document":[
{
"id":601,
"project_id":2392,
"parent_type":"Document",
"type":"Land Details",
"name":"FORMS.pdf",
"link":"https://....HotelsFORMS.pdf"
}
],
"Miscellaneous Approvals":[
{
"id":604,
"project_id":2392,
"parent_type":
"Miscellaneous Approvals",
"type":"Not Reported",
"name":"Report Part 1.pdf",
"link":"https://...Report Part 1.pdf"
}
]
}
};
从对象获取数据并分配给无序
变量
const unordered = data.data;
声明新变量已排序
const ordered = {};
Object.keys
将从无序对象
获取keys
数组,然后对键应用排序函数
对于升序排序
。
然后我们将在键数组
上执行forEach
循环,并将键的值分配给有序对象
;
Object.keys(unordered).sort().forEach(function(key) {
ordered[key] = unordered[key];
});
console.log(ordered);
关于javascript - 在javascript中对数组的索引进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54478845/