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

时间:2018-12-03 12:31:17

标签: javascript

嗨,我想对javascript中的对象数组进行排序。下面是示例数据。

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: []
        }]
    }
];

应基于属性“名称”对该数组进行排序,子对象应基于“名称”属性再次进行排序。

因此,预期的输出如下所示,并且希望保留其他属性以及子级中的电子邮件属性。

名字      some_name   c_name      儿童      nested_child    名称       child_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;
};

};

有人可以帮助我如何继续执行此操作。谢谢。

3 个答案:

答案 0 :(得分:0)

假设您的孩子是数组,而不是按照您的示例的对象:

scikit-learn

一种快速的方法是:

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: []
            }]
        }
    ];

答案 1 :(得分:0)

You can simply use the sort() method

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

答案 2 :(得分:0)

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).

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));