简体   繁体   English

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

[英]How to sort the array of objects in javascript?

Hi i want to sort an array of objects in javascript. 嗨,我想在javascript中对对象数组进行排序。 Below is the example data. 以下是示例数据。

const example = [
    {
        name: "c_name",
        children: [{
            name: "child",
            email: "child1@dev.com",
            children: [{
                name: "nested_child",
                email: "nestedchild1@dev.com",
            }]
        }]
    },
    {
        name: "a_name",
        children: [{
            name: "some_name",
            email: "some_name@dev.com",
            children: []
        }]
    },
    {
        name: "name",
        children: [{
            name: "child_name",
            email: "child_name@dev.com",
            children: []
        }]
    }
];

Should sort this array based on property 'name' and the children object should be sorted again based on 'name' property. 应该基于属性“名称”对该数组进行排序,并且应该基于“名称”属性再次对子对象进行排序。

So the expected output is like below, and would like to retain other properties as well like email property in children. 因此,预期的输出如下所示,并且希望保留其他属性以及子级中的电子邮件属性。

a_name some_name c_name child nested_child name child_name a_name some_name c_name子代nested_child名称child_name

What i have done...i have a sort function that sorts the array by name property. 我做了什么...我有一个排序功能,按名称属性对数组进行排序。 however dont know how to sort the children object with name property. 但是不知道如何使用名称属性对子对象进行排序。

const sorted_example = example.sort(this.sort_by_name()); const sorted_example = example.sort(this.sort_by_name());

sort_by_name = () => {
return (a, b) => {
    let result;
    const a_value = a.name.toLowerCase();
    const b_value = b.name.toLowerCase();
    if (a_value > b_value) {
        result = 1;
    } else if (a_value < b_value) {
        result = -1;
    } else {
        result = 0;
    }

    return result;
};

}; };

Could someone help me how to continue with this. 有人可以帮助我如何继续执行此操作。 thanks. 谢谢。

assuming your children are arrays instead of objects as per your example: 假设您的孩子是数组而不是根据您的示例的对象:

const example = [
        {
            name: "c_name",
            children: [{
                name: "child",
                email: "child1@dev.com",
                children: [{
                    name: "nested_child",
                    email: "nestedchild1@dev.com",
                }]
            }]
        },
        {
            name: "a_name",
            children: [{
                name: "some_name",
                email: "some_name@dev.com",
                children: []
            }]
        },
        {
            name: "name",
            children: [{
                name: "child_name",
                email: "child_name@dev.com",
                children: []
            }]
        }
    ];

a quick way would be: 一种快速的方法是:

    example
        .sort((a, b) => a.name.localeCompare(b.name))
        .map(m => {
            return {
                name: m.name,
                children: m.children.sort((a, b) => a.name.localeCompare(b.name))
            };
        });

您可以简单地使用sort()方法

example.sort((el, q) => el.name.localeCompare(q.name))

The previous answers got you there most of the way but you need to sort again if an item has children. 先前的答案大多数情况下都可以帮助您,但是如果某项包含子项,则需要重新排序。 In my example I don't mutate the original array (use .slice to make a shallow copy so .sort doesn't mutate). 在我的示例中,我不.slice原始数组(使用.slice进行浅拷贝,因此.sort不会.slice )。

 const example = [{"name":"c_name","children":[{"name":"child","email":"child1@dev.com","children":[{"name":"nested_child","email":"nestedchild1@dev.com"}]},{"name":"b"},{"name":"a"}]},{"name":"a_name","children":[{"name":"some_name","email":"some_name@dev.com","children":[]}]},{"name":"name","children":[{"name":"child_name","email":"child_name@dev.com","children":[]}]}]; const sortRecursive = (data) => { const recur = (arr) => arr .slice() .sort((a, b) => a.name.localeCompare(b.name)) //check each item to see if it has children that is an array .map( (item) => //if item has children that is an array then sort that // and it's childrens childrens children Array.isArray(item.children) ? { ...item, children: recur(item.children), } : item, //no children, just return the item ); return recur(data); }; //note that sortRecursive does not change example but returns a new array // that is sorted console.log(sortRecursive(example)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM