我正在尝试在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])
})
}
我遇到类似这样的错误:
未捕获(承诺)TypeError:无法读取未定义的属性'subscProDocument'
我已经在data()中定义了这个subscProDocument
并初始化为一个空数组。帮我解决这个问题。谢谢
答案 0 :(得分:1)
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"
}
]
}
};
从对象获取数据并分配给unordered
变量
const unordered = data.data;
声明新变量ordered
const ordered = {};
Object.keys
将从keys
获取unordered object
的数组,然后将sort function
应用于ascending sort
的键。
然后我们将在forEach
上执行array of keys
循环,并将带有键的值分配给ordered object
;
Object.keys(unordered).sort().forEach(function(key) {
ordered[key] = unordered[key];
});
console.log(ordered);
答案 1 :(得分:0)
这是解决方案:
var obj={
"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"
}
]
}
};
function map(it){
var item={};
item[it]=obj.data[it];
return item;
}
console.log(Object.keys(obj.data).sort().map(map));
答案 2 :(得分:0)
您的data.data
对象如果要订购其项目,应为数组,因为您不能依赖Object
中键的顺序,因为它是不保证。
这是post about key ordering in JS objects
您可以执行以下排序并转换为一行的数组:
data.data = orderedKeys.map(key => ({ [key]: data.data[key] }));
这将为您提供:
{
"data": [
{
"Document": [{
"id": 601,
"project_id": 2392,
...
}]
},
{
"Environmental Clearance": [{
"id": 602,
"project_id": 2392,
...
}]
},
{
"Real Estate Regulatory Act": [{
"id": 603,
"project_id": 2392,
...
}]
},
{
"Miscellaneous Approvals": [{
"id": 604,
"project_id": 2392,
...
}]
}
]
}
这是一个有效的示例:
const 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 orderedKeys = ['Document', 'Environmental Clearance', 'Real Estate Regulatory Act', 'Miscellaneous Approvals'];
data.data = orderedKeys.map(key => ({ [key]: data.data[key] }));
console.log(data)
答案 3 :(得分:0)
您需要以Array#forEach
的this
身份引用thisArg
sortDocs.forEach(function(key) {
this.subscProDocument[key] = result.push(docs[key]);
}, this);
或采用一个arrow function,它占用了外部范围的this
。
sortDocs.forEach(key => this.subscProDocument[key] = result.push(docs[key]));
答案 4 :(得分:0)
let obj = {
"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 orderedObj = { data: {} };
Object.keys(obj.data).sort().forEach(function(key) {
orderedObj.data[key] = obj.data[key];
});