简体   繁体   English

如何对对象数组进行排序

[英]How to sort array of objects

I need your help to understand how to sort array of objects with another array inside each of object. 我需要您的帮助,以了解如何对对象数组与每个对象内的另一个数组进行排序。 I have response from api with array bunch of objects like below: 我从api收到带有数组对象数组的响应,如下所示:

[
    {
        "name": "[C US Equity] CITIGROUP INC",
        "stats": [
            {
                "label": "DTD",
                "value": null
            },
            {
                "label": "MTD",
                "value": null
            },
            {
                "label": "YTD",
                "value": 0.0536530913792681
            },
            {
                "label": 2016,
                "value": 0.18102519526139493
            },
            {
                "label": 2015,
                "value": -0.012946569977188238
            },
            {
                "label": 2014,
                "value": null
            }
        ]
    }...
]

I should be sort it by value of label "YTD" exclude of null value. 我应该按标签“ YTD”的值(不包含空值)对它进行排序。 Any help are welcome 欢迎任何帮助

You can use array#sort method 您可以使用array#sort方法

 var oldArray = [{ "name": "[C US Equity] CITIGROUP INC", "stats": [{ "label": "DTD", "value": null }, { "label": "MTD", "value": null }, { "label": "YTD", "value": 0.0536530913792681 }, { "label": 2016, "value": 0.18102519526139493 }, { "label": 2015, "value": -0.012946569977188238 }, { "label": 2014, "value": null } ] }]; oldArray[0].stats.sort(function(a, b) { return a.label - b.label; }) console.log(oldArray) 

You can use collection sort method provided by lodash. 您可以使用lodash提供的集合排序方法。 https://lodash.com/docs/4.17.4#sortBy https://lodash.com/docs/4.17.4#sortBy

var users = [{
    "name": "[C US Equity] CITIGROUP INC",
    "stats": [
        {
            "label": "DTD",
            "value": null
        },
        {
            "label": "MTD",
            "value": null
        },
        {
            "label": "YTD",
            "value": 0.0536530913792681
        },
        {
            "label": null,
            "value": 0.18102519526139493
        },
        {
            "label": "2015",
            "value": -0.012946569977188238
        },
        {
            "label": "dtd",
            "value": null
        }
    ]
}]

console.log(_.sortBy(users[0].stats, [function(o) { if(o.label){return  o.label.toUpperCase();} }]));

This takes case of null values too. 这也需要空值的情况。

I think this post may help you. 我认为这篇文章可能会对您有所帮助。
Take a look of it, it teaches you about how compare works. 看看它,它教您如何比较。

All you need to do is to put that JSON thing into an object array, then compare the value of YTD(make two for-loops, result [i] .stats [j] .label === 'YTD') to make the array to re-order it. 您需要做的就是将JSON内容放入对象数组,然后比较YTD的值(创建两个for循环,结果[i] .stats [j] .label ==='YTD')以使数组重新排序。

A comparison function is work like below(extracted from the above link) 比较功能的工作方式如下(摘自上述链接)

function CompareNumbers( v1, v2 ) {
    if ( v1 < v2 ) return -1;  // v1 goes nearer to the top
    if ( v1 > v2 ) return 1;   // v1 goes nearer to the bottom
    return 0;                  // both are the same
}

when it return -1(or sometimes false), the former one will be pushed to the front of your array, meanwhile 1 or true will push the former one to the back. 当它返回-1(或有时为false)时,前一个将被推到数组的前面,而1或true会将前一个推到数组的后面。 And 0 (means not true or false, or you can handle the exception by yourself, push back or forward) will happen nothing. 并且0(表示不是true或false,或者您可以自己处理异常,后退或前进)将什么都不会发生。

But becare if your array exist two 'YTD' labelled value, if it happens it will make two value to appear in the same data. 但是请注意,如果数组中存在两个带有“ YTD”标记的值,如果发生这种情况,它将使两个值出现在同一数据中。

This may help you in sorting the data while you cannot directly get a sorted data from your db server. 当您无法直接从数据库服务器获取排序后的数据时,这可能有助于您对数据进行排序。
Oh, forgot to say, the compare function can compare the whole object.(of course you need to extract something like this ---> object[index].['Something your needed']), not only numbers. 哦,忘了说了,compare函数可以比较整个对象。(当然,您需要提取类似---> object [index]。['Something your needed']]的对象),不仅是数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM