Returning data after sorting through multiple arrays

扶醉桌前 提交于 2020-03-25 23:50:48

问题


I have nested arrays within an object, and I need to extract these values, and sort them into alphabetical order which will then be displayed in a table.

I am using the localeCompare method but it is returning:

Cannot read property 'localeCompare' of null

To be honest, I am not sure I am approaching this correctly. Using sort you can compare a to b to sort them into alphabetical order. I am getting confused how to compare the values from the arrays within a and b. I am using the first sort on tableData to access the array, and the using a second sort to compare the values that I pushed to array clientRefArr

if(params.sorting().clientRef) {

        var clientRefArr = [];

        tableData.sort(function(a, b){

            a.renewalUIs.forEach(function(data, i){
                clientRefArr.push(data.patentUI.clientRef);
            })

            return clientRefArr.sort(function(a, b){
                console.log(a.localeCompare(b))
                // return a.localeCompare(b)
            })

        })

    orderedData = tableData;

}

return orderedData;

Question

Why is the error Cannot read property 'localeCompare' of null being returned?Am I approaching this issue completely wrong?

//JSON

[
 0: {
     transRef: "IX1000013"
     renewalProgress: 30
     renewalUIs: [
        0: {
            patentUI: {
               business: null
               clientRef: P0101011 // this is the required value 
            }
            renewalAttemptsMade: 1
            renewalDueDate: 1514764740000
         }
     ]

   },
 1: {
     transRef: "IX100333"
     renewalProgress: 55
     renewalUIs: [
        0: {
            patentUI: {
               business: null
               clientRef: P0101011 // this is the required value 
            }
            renewalAttemptsMade: 1
            renewalDueDate: 1514764740000
         },
        1: {
            patentUI: {
               business: null
               clientRef: P5551011 // this is the required value 
            }
            renewalAttemptsMade: 3
            renewalDueDate: 174834740000
         }
     ]

   }
]

回答1:


You could take a default value for both parts

(a || '').localeCompare(b || '')

for sorting the null values or even all falsy values to top.

An attempt with the given data (I changed some value, to get some sorting).

It now sorts the inner arrays renewalUIs first and then it takes the first element for sorting the outer array.

var array = [{ transRef: "IX1000013", renewalProgress: 30, renewalUIs: [{ patentUI: { business: null, clientRef: "P9101011" }, renewalAttemptsMade: 1, renewalDueDate: 1514764740000 }] }, { transRef: "IX100333", renewalProgress: 55, renewalUIs: [{ patentUI: { business: null, clientRef: "P7101011" }, renewalAttemptsMade: 1, renewalDueDate: 1514764740000 }, { patentUI: { business: null, clientRef: "P5551011" }, renewalAttemptsMade: 3, renewalDueDate: 174834740000 }] }];

array.forEach(function (o) {
    o.renewalUIs.sort(function (a, b) {
        return a.patentUI.clientRef.localeCompare(b.patentUI.clientRef);
    });
});

array.sort(function (a, b) {
    return a.renewalUIs[0].patentUI.clientRef.localeCompare(b.renewalUIs[0].patentUI.clientRef);
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }



回答2:


IF my interpretation is correct, it'd be way easier to first put everything in an single array and then sort it as such just use loops for the first task and a normal sort for the second, nested sorts are really just a bad idea.

// let's assume your data is in unorderedData
var clientUIs = [];
for (var i = 0; i < unorderedData.length; i++){
  for (var j = 0 ; j < unorderedData[i]["renewalUIs"].length; j++){
     // add each UI to your list individually.
     clientUIs.push(unorderedData[i]["renewalUIs"][j]["patentUI"]);
  }
}
clientUIs.sort() //just sort everything
tableData = clientUIs;

If "renewalUIs" or "patentUIs" arent constant you can iterate over the keys of the dictionary.



来源:https://stackoverflow.com/questions/47333488/returning-data-after-sorting-through-multiple-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!