按嵌套子数组排序 arr

Sort arr by nested sub-array

这是我的地址:

var arr = [
    {
        "name": "ASD",
        "properties": [
            {
                "name": "TypeId",
                "nominalValue": "AC101"
            },
            {
                "name": "Description",
                "nominalValue": "text here"
            }
        ]
    },
    {
        "name": "EWQ",
        "properties": [
            {
                "name": "TypeId",
                "nominalValue": "AB123"
            },
            {
                "name": "Description",
                "nominalValue": "text here"
            }
        ]
    }
]

我想通过在属性 arr 中给出 name 对其进行排序,它 returns 它通过 nominalValue

进行排序

像这样:

var sortedArr = sortListByPropertyName(arr, 'TypeId'); //Sorts list by 'AB123', 'AC101'..

我的数据集很大,所以尽可能优化它很重要。我可以将 _.sortBy 与一些聪明的 chains 一起使用吗?

你可以试试这个:

function sortListByPropertyName(arr, propName) {
  return _.sortBy(arr, function(obj) {
    return
    _.find(obj.properties, function(prop) {
      return prop.name == propName
    }).nominalValue;
  });
}