gpt4 book ai didi

javascript - 使用 sortBy 方法排序在下划线中不起作用

转载 作者:行者123 更新时间:2023-11-30 08:27:02 24 4
gpt4 key购买 nike

我正在尝试使用下划线 sortBy 方法对对象数组进行排序,json 如下

[
{
"styleId": 91,
"styleName": "Style Label",
"dataSourceId": 1,
"dataSourceName": null,
"stationeryId": 1,
"stationeryName": null,
"styleType": 1,
"styleTypeName": "Labels",
"isActive": true,
"isPlainPaper": true,
"isSystemMaintained": false,
"isPublished": true,
"designerUrl": null,
"sourceStyleId": 0,
"rowIndicatorCode": 2,
"isDefault": null,
"hasElement": true,
"styleDesign": null,
"createdBy": "real",
"createdDate": 1494217733790,
"modifiedBy": "superuser",
"modifiedDate": 1494388952450,
"rowVersion": "AAAAAAIC8ls="
},
{
"styleId": 69,
"styleName": "irtest",
"dataSourceId": 1,
"dataSourceName": null,
"stationeryId": 16,
"stationeryName": null,
"styleType": 1,
"styleTypeName": "Labels",
"isActive": true,
"isPlainPaper": false,
"isSystemMaintained": false,
"isPublished": true,
"designerUrl": null,
"sourceStyleId": 0,
"rowIndicatorCode": 2,
"isDefault": null,
"hasElement": false,
"styleDesign": null,
"createdBy": "arun",
"createdDate": 1493288218843,
"modifiedBy": "real",
"modifiedDate": 1494062410107,
"rowVersion": "AAAAAAHZaDk="
},
{
"styleId": 21,
"styleName": "new style-copy",
"dataSourceId": 1,
"dataSourceName": null,
"stationeryId": 5,
"stationeryName": null,
"styleType": 1,
"styleTypeName": "Labels",
"isActive": true,
"isPlainPaper": false,
"isSystemMaintained": false,
"isPublished": true,
"designerUrl": null,
"sourceStyleId": 0,
"rowIndicatorCode": 2,
"isDefault": null,
"hasElement": true,
"styleDesign": null,
"createdBy": "Nandita",
"createdDate": 1493186162607,
"modifiedBy": "Nandita",
"modifiedDate": 1493186173807,
"rowVersion": "AAAAAAEXEwI="
},
{
"styleId": 97,
"styleName": "style 1",
"dataSourceId": 1,
"dataSourceName": null,
"stationeryId": 1,
"stationeryName": null,
"styleType": 1,
"styleTypeName": "Labels",
"isActive": true,
"isPlainPaper": true,
"isSystemMaintained": false,
"isPublished": true,
"designerUrl": null,
"sourceStyleId": 0,
"rowIndicatorCode": 2,
"isDefault": null,
"hasElement": true,
"styleDesign": null,
"createdBy": "real",
"createdDate": 1494646737753,
"modifiedBy": "real",
"modifiedDate": 1494647127567,
"rowVersion": "AAAAAAIxLKM="
}
]

这是我的示例 json,我正在尝试按字段 styleName 对这个 json 进行排序,例如`

var result = _.sortBy(ctrl.styles, function (o) { return o.styleName; });

得到的结果如下

[
{
"styleId": 21,
"styleName": "new style-copy",
"dataSourceId": 1,
"dataSourceName": null,
"stationeryId": 5,
"stationeryName": null,
"styleType": 1,
"styleTypeName": "Labels",
"isActive": true,
"isPlainPaper": false,
"isSystemMaintained": false,
"isPublished": true,
"designerUrl": null,
"sourceStyleId": 0,
"rowIndicatorCode": 2,
"isDefault": null,
"hasElement": true,
"styleDesign": null,
"createdBy": "Nandita",
"createdDate": 1493186162607,
"modifiedBy": "Nandita",
"modifiedDate": 1493186173807,
"rowVersion": "AAAAAAEXEwI="
},
{
"styleId": 69,
"styleName": "irtest",
"dataSourceId": 1,
"dataSourceName": null,
"stationeryId": 16,
"stationeryName": null,
"styleType": 1,
"styleTypeName": "Labels",
"isActive": true,
"isPlainPaper": false,
"isSystemMaintained": false,
"isPublished": true,
"designerUrl": null,
"sourceStyleId": 0,
"rowIndicatorCode": 2,
"isDefault": null,
"hasElement": false,
"styleDesign": null,
"createdBy": "arun",
"createdDate": 1493288218843,
"modifiedBy": "real",
"modifiedDate": 1494062410107,
"rowVersion": "AAAAAAHZaDk="
},
{
"styleId": 91,
"styleName": "Style Label",
"dataSourceId": 1,
"dataSourceName": null,
"stationeryId": 1,
"stationeryName": null,
"styleType": 1,
"styleTypeName": "Labels",
"isActive": true,
"isPlainPaper": true,
"isSystemMaintained": false,
"isPublished": true,
"designerUrl": null,
"sourceStyleId": 0,
"rowIndicatorCode": 2,
"isDefault": null,
"hasElement": true,
"styleDesign": null,
"createdBy": "real",
"createdDate": 1494217733790,
"modifiedBy": "superuser",
"modifiedDate": 1494388952450,
"rowVersion": "AAAAAAIC8ls="
},
{
"styleId": 97,
"styleName": "style 1",
"dataSourceId": 1,
"dataSourceName": null,
"stationeryId": 1,
"stationeryName": null,
"styleType": 1,
"styleTypeName": "Labels",
"isActive": true,
"isPlainPaper": true,
"isSystemMaintained": false,
"isPublished": true,
"designerUrl": null,
"sourceStyleId": 0,
"rowIndicatorCode": 2,
"isDefault": null,
"hasElement": true,
"styleDesign": null,
"createdBy": "real",
"createdDate": 1494646737753,
"modifiedBy": "real",
"modifiedDate": 1494647127567,
"rowVersion": "AAAAAAIxLKM="
}
]

其中ctrl.styles是上面的JSON,是不是哪里做错了?

最佳答案

var result =  _.sortBy(ctrl.styles, function (a) { return a.styleName.toUpperCase(); });

我遇到的问题是 styleName 的大小写不同,将 styleName 转换为大写解决了这个问题。 Yashar的回答也解决了问题

关于javascript - 使用 sortBy 方法排序在下划线中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43949332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com