简体   繁体   中英

Sugar.js not working

I have this lngArr:

[
    {
        "data": {
            "18746": 20.75,
            "id": "18746",
            "impression": 20.75
        },
        "name": "Missing"
    },
    {
        "data": {
            "18748": 77.91,
            "id": "18748",
            "impression": 77.91
        },
        "name": "English"
    },
    {
        "data": {
            "18749": 0.02,
            "id": "18749",
            "impression": 0.02
        },
        "name": "Spanish"
    },
    {
        "data": {
            "18751": 0.31,
            "id": "18751",
            "impression": 0.31
        },
        "name": "Arabic"
    },
    {
        "data": {
            "18753": 0.01,
            "id": "18753",
            "impression": 0.01
        },
        "name": "Chinese"
    },
    {
        "data": {
            "18757": 0.01,
            "id": "18757",
            "impression": 0.01
        },
        "name": "Dutch"
    },
    {
        "data": {
            "18760": 0.01,
            "id": "18760",
            "impression": 0.01
        },
        "name": "French"
    },
    {
        "data": {
            "18763": 0.69,
            "id": "18763",
            "impression": 0.69
        },
        "name": "Hebrew"
    },
    {
        "data": {
            "18764": 0.08,
            "id": "18764",
            "impression": 0.08
        },
        "name": "Hindi"
    },
    {
        "data": {
            "18771": 0.2,
            "id": "18771",
            "impression": 0.2
        },
        "name": "Korean"
    },
    {
        "data": {
            "18772": 0.01,
            "id": "18772",
            "impression": 0.01
        },
        "name": "Latvian"
    },
    {
        "data": {
            "18786": 0.01,
            "id": "18786",
            "impression": 0.01
        },
        "name": "Turkish"
    }
]

and here is my sorting code:

lngArr.sortBy(function(lng) {
                return -lng.data.impression;
            });

I don't know why it is not working. here is the documentation .

The docs might be a bit unclear when stating that Array is returned, but the source clears this up:

A new (sorted) array is returned.

So you likely want to do

lngArr = lngArr.sortBy(function(lng) {
    return -lng.data.impression;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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