I'm trying to build a small application on Vuejs
where I'm having a set of array which comes up through an api response which gives following output:
{
"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"
}
]
}
}
I want to sort this javascript as per the indexes with respect to following array:
['Document', 'Environmental Clearance', 'Real Estate Regulatory Act', 'Miscellaneous Approvals']
So my final result will be:
{
"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"
}
]
}
}
My code currently look like:
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])
})
}
I get error of something like this:
Uncaught (in promise) TypeError: Cannot read property 'subscProDocument' of undefined
I already defined this subscProDocument
in data() and initialized as an empty array. Help me out with this. Thanks