通过多个数组排序后返回数据 [英] Returning data after sorting through multiple arrays

查看:85
本文介绍了通过多个数组排序后返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在对象内嵌套了数组,我需要提取这些值,然后将它们按字母顺序排序,然后将其显示在表格中。

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我正在使用 localeCompare 方法,但它返回:

I am using the localeCompare method but it is returning:

Cannot read property 'localeCompare' of null

老实说,我不确定我是否正确地解决了这个问题。使用 sort 可以比较 a b 进行排序按字母顺序排列。我很困惑如何比较 a b 中数组的值。我正在使用 tableData 上的第一个 sort 访问数组,并使用第二个排序比较我推送到数组 clientRefArr

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

为什么错误无法读取null的属性'localeCompare'我是否完全解决此问题?

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
         }
     ]

   }
]


推荐答案

您可以为两个部分都使用默认值

You could take a default value for both parts

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

用于对进行排序空值,甚至所有虚假

尝试使用给定的数据(我更改了一些值以进行排序)。

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

现在它首先对内部数组 renewalUIs 进行排序,然后采用第一个元素对外部数组进行排序。

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

这篇关于通过多个数组排序后返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆