React Javascript-根据内部嵌套数组值对数组进行排序

时间:2018-06-21 11:38:01

标签: javascript arrays reactjs sorting nested

我有一个需要根据sortPosition值对特定rotaTypeId值进行排序的数组。

当前rotaTypeId1ebe7766-4a6b-4157-a998-00ebae24d662,因此预期结果将首先显示Sally,然后显示Joe。

我尝试遍历外部数组,并为每个元素获取其rotaTypeAccountEmployees内部数组。然后检查当前的rotaTypeId值是否匹配。

据我所知,我不确定如何返回更新/排序的数组。

这是我的数组:

[
    {
        "object": "accountEmployee",
        "accountEmployeeId": "c80b2d75-6091-423c-b51b-41cef265046a",
        "employee": {
            "object": "employee",
            "employeeId": "c3832cff-ac4c-4133-ad29-a00ca8fd25f6",
            "firstName": "Joe",
            "surname": "Bloggs",
            "email": "joe@bloggs.com"
        },
        "salary": 16286.40,
        "hourlyRate": 7.83,
        "weeklyContractHours": 40,
        "rotaTypeAccountEmployees": [
            {
                "object": "rotaTypeAccountEmployee",
                "rotaTypeId": "1ebe7766-4a6b-4157-a998-00ebae24d662",
                "sortPosition": 2
            },
            {
                "object": "rotaTypeAccountEmployee",
                "rotaTypeId": "01d8ec46-d1cf-49e2-b992-840dfdb03a83",
                "sortPosition": 1
            }
        ]
    },
    {
        "object": "accountEmployee",
        "accountEmployeeId": "bdde68a4-7df0-431b-b108-db5c26ca7208",
        "employee": {
            "object": "employee",
            "employeeId": "724c4c4c-978d-4f62-9345-28219153e728",
            "firstName": "Sally",
            "surname": "Bloggs",
            "email": "sally@bloggs.com"
        },
        "salary": 16286.40,
        "hourlyRate": 7.83,
        "weeklyContractHours": 40,
        "rotaTypeAccountEmployees": [
            {
                "object": "rotaTypeAccountEmployee",
                "rotaTypeId": "1ebe7766-4a6b-4157-a998-00ebae24d662",
                "sortPosition": 1
            },
            {
                "object": "rotaTypeAccountEmployee",
                "rotaTypeId": "01d8ec46-d1cf-49e2-b992-840dfdb03a83",
                "sortPosition": 2
            }
        ]
    }
]

3 个答案:

答案 0 :(得分:1)

尝试关注

let arr = [{"object":"accountEmployee","accountEmployeeId":"c80b2d75-6091-423c-b51b-41cef265046a","employee":{"object":"employee","employeeId":"c3832cff-ac4c-4133-ad29-a00ca8fd25f6","firstName":"Joe","surname":"Bloggs","email":"joe@bloggs.com"},"salary":16286.4,"hourlyRate":7.83,"weeklyContractHours":40,"rotaTypeAccountEmployees":[{"object":"rotaTypeAccountEmployee","rotaTypeId":"1ebe7766-4a6b-4157-a998-00ebae24d662","sortPosition":2},{"object":"rotaTypeAccountEmployee","rotaTypeId":"01d8ec46-d1cf-49e2-b992-840dfdb03a83","sortPosition":1}]},{"object":"accountEmployee","accountEmployeeId":"bdde68a4-7df0-431b-b108-db5c26ca7208","employee":{"object":"employee","employeeId":"724c4c4c-978d-4f62-9345-28219153e728","firstName":"Sally","surname":"Bloggs","email":"sally@bloggs.com"},"salary":16286.4,"hourlyRate":7.83,"weeklyContractHours":40,"rotaTypeAccountEmployees":[{"object":"rotaTypeAccountEmployee","rotaTypeId":"1ebe7766-4a6b-4157-a998-00ebae24d662","sortPosition":1},{"object":"rotaTypeAccountEmployee","rotaTypeId":"01d8ec46-d1cf-49e2-b992-840dfdb03a83","sortPosition":2}]}];
let id = "1ebe7766-4a6b-4157-a998-00ebae24d662";
arr.sort((a,b) => {
  return a.rotaTypeAccountEmployees.find(({rotaTypeId}) => id === rotaTypeId).sortPosition - b.rotaTypeAccountEmployees.find(({rotaTypeId}) => id === rotaTypeId).sortPosition
});
console.log(arr);

答案 1 :(得分:0)

您可以使用Ramda库,并通过函数组合轻松实现它:

List<Observable> productViewObservables = new ArrayList<>();

for (ProductEnricher enricher : orchestrationStrategy.getEnrichers()) {
      productViewObservables.add(enricher.asyncEnrich(productView, productId); }

答案 2 :(得分:0)

我认为Nikhil Aggarwal解决方案更快,更短,但是很高兴知道一些算法实现,这里是Quick Sort实现:

var criteria = "1ebe7766-4a6b-4157-a998-00ebae24d662";

function partition(arr, low, high)
    {
        var pivot = getValue(arr[high], criteria); 
        var i = (low-1); 
        for (let j=low; j<high; j++)
        {
            if (getValue(arr[j], criteria) <= pivot)
            {
                i++;

                let temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        var temp = arr[i+1];
        arr[i+1] = arr[high];
        arr[high] = temp;

        return i+1;
    }

    function quickSort(arr, low,high)
    {
        if (low < high)
        {
            let pi = partition(arr, low, high);
            quickSort(arr, low, pi-1);
            quickSort(arr, pi+1, high);
        }
    }

    function getValue(element, criteria){
        let value = 0;
        element.rotaTypeAccountEmployees.map(o=>{
           if(o.rotaTypeId==criteria){
            value =  o.sortPosition;   
           } 
        });
        return value;
    }


quickSort(arr,0,arr.length-1);
console.log(arr);

此方法的优点是getValue()函数可以更改以适合许多数据结构。因此partition()quickSort()可以保持不变。