在javascript中对数组的索引进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在javascript中对数组的索引进行排序相关的知识,希望对你有一定的参考价值。

我正在尝试在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:无法读取undefined的属性'subscProDocument'

我已经在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"
            }
        ]
    }
};

从Object获取数据并分配给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);
另一答案
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];
});
另一答案

你需要参考this作为thisArgArray#forEach

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]));
另一答案

如果你想订购它的物品,你的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)
另一答案

这是解决方案:

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",
               

以上是关于在javascript中对数组的索引进行排序的主要内容,如果未能解决你的问题,请参考以下文章

在Javascript中对地址数组进行排序[重复]

在javascript中对混合服装尺寸的数组进行排序

如何在javascript中对多维数组进行排序

如何在javascript中对嵌套的对象数组进行排序?

在javascript中对具有偏移量的对象数组进行排序

如何在JavaScript中对二维数组进行排序?