我有如下对象数组:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ABCD_Create([DataSourceRequest] DataSourceRequest request, MyViewModel my, short abcd_id)
{
if (my != null && ModelState.IsValid)
{
MY tm = Mapper.Map<MyViewModel>(my);
tm.abcd_id = abcd_id;
try
{
repo.Create(tm);
my.primarykey_id = tm.primarykey_id; ///This is most important
}
catch (Exception ex)
{
ModelState.AddModelError("Duplicate Value Found", string.Format("error: {0} already exists", my.abcd));
}
}
else
{
ModelState.AddModelError("Error", "Not valid please send data again");
}
return Json(new[] { my }.ToDataSourceResult(request, ModelState));
}
我希望用profData=[{"Details":{"CODE":"PAT4PAS","DESCRIPTION":"PASTIE 4N20 12 X 175G","LOCATION":"FREEZER","UNITS":"BOX","WAREHOUSE":"00","AVAILABLE":"15.0000","ON_HAND":"15.0000","BRAND":"4N20","PRICE1":"18.80","TAX_CODE1":"1","PROD_CLASS":"FROZ"}},{"Details":{"CODE":"PAT4PAS","DESCRIPTION":"PASTIE 4N20 12 X 175G","LOCATION":"FREEZER","UNITS":"BOX","WAREHOUSE":"00","AVAILABLE":"15.0000","ON_HAND":"15.0000","BRAND":"4N20","PRICE1":"18.80","TAX_CODE1":"1","PROD_CLASS":"FROZ"}},{"Details":{"CODE":"OMKM","DESCRIPTION":"SAUCE KETCHUP MANIS 620ML [6]","LOCATION":"DRY","UNITS":"EACH","WAREHOUSE":"00","AVAILABLE":"187.0000","ON_HAND":"189.0000","BRAND":"ABC","PRICE1":"3.40","TAX_CODE1":"-1","PROD_CLASS":"DRY"}},{"Details":{"CODE":"BAXSAL","DESCRIPTION":"SALSA MEXICAN 2LTR [6]","LOCATION":"DRY","UNITS":"EACH","WAREHOUSE":"00","AVAILABLE":"439.0000","ON_HAND":"445.0000","BRAND":"AGE","PRICE1":"9.20","TAX_CODE1":"-1","PROD_CLASS":"DRY"}},{"Details":{"CODE":"BAXSEMI","DESCRIPTION":"TOMATO SEMI DRIED 2KG [4]","LOCATION":"REFRIDGERATOR","UNITS":"EACH","WAREHOUSE":"00","AVAILABLE":"55.0000","ON_HAND":"59.0000","BRAND":"AGE","PRICE1":"21.10","TAX_CODE1":"-1","PROD_CLASS":"CHILL"}},{"Details":{"CODE":"BAXSUN","DESCRIPTION":"TOMATO SUN DRIED 2KG [4]","LOCATION":"REFRIDGERATOR","UNITS":"EACH","WAREHOUSE":"00","AVAILABLE":"39.0000","ON_HAND":"39.0000","BRAND":"AGE","PRICE1":"15.20","TAX_CODE1":"-1","PROD_CLASS":"CHILL"}},{"Details":{"CODE":"AMFBAM","DESCRIPTION":"FLOUR - AM SUP BAKERS 12.5KG","LOCATION":"REFRIDGERATOR","UNITS":"BAG","WAREHOUSE":"00","AVAILABLE":"259.0000","ON_HAND":"339.0000","BRAND":"ALLIE","PRICE1":"13.70","TAX_CODE1":"-1","PROD_CLASS":"CHILL"}},{"Details":{"CODE":"ALPKKC","DESCRIPTION":"KRANSKY - CHEESE 2.5KG [6]","LOCATION":"REFRIDGERATOR","UNITS":"EACH","WAREHOUSE":"00","AVAILABLE":"27.0000","ON_HAND":"27.0000","BRAND":"ALP","PRICE1":"19.30","TAX_CODE1":"-1","PROD_CLASS":"CHILL"}},{"Details":{"CODE":"ABTDP","DESCRIPTION":"TOOTHPICK - DOUBLE POINT 1000","LOCATION":"DRY","UNITS":"EACH","WAREHOUSE":"00","AVAILABLE":"12.0000","ON_HAND":"12.0000","BRAND":"ALPEN","PRICE1":"1.60","TAX_CODE1":"1","PROD_CLASS":"DRY"}}]
,
我用下划线js尝试了它,但没有得到解决方案。
PROD_CLASS
但不会使用var fullySorted = _.sortBy(profData, 'PROD_CLASS');
进行排序。
任何人都可以帮助我。
提前致谢。
答案 0 :(得分:2)
由于PROD_CLASS
不是数组元素的直接属性,因此不起作用。
_.sortBy(profData, obj => obj.Details.PROD_CLASS);
这与
相同_.sortBy(arr, function(obj) {
return obj.Details.PROD_CLASS;
});
var profData = [{
"Details": {
"CODE": "PAT4PAS",
"DESCRIPTION": "PASTIE 4N20 12 X 175G",
"LOCATION": "FREEZER",
"UNITS": "BOX",
"WAREHOUSE": "00",
"AVAILABLE": "15.0000",
"ON_HAND": "15.0000",
"BRAND": "4N20",
"PRICE1": "18.80",
"TAX_CODE1": "1",
"PROD_CLASS": "FROZ"
}
}, {
"Details": {
"CODE": "PAT4PAS",
"DESCRIPTION": "PASTIE 4N20 12 X 175G",
"LOCATION": "FREEZER",
"UNITS": "BOX",
"WAREHOUSE": "00",
"AVAILABLE": "15.0000",
"ON_HAND": "15.0000",
"BRAND": "4N20",
"PRICE1": "18.80",
"TAX_CODE1": "1",
"PROD_CLASS": "FROZ"
}
}, {
"Details": {
"CODE": "OMKM",
"DESCRIPTION": "SAUCE KETCHUP MANIS 620ML [6]",
"LOCATION": "DRY",
"UNITS": "EACH",
"WAREHOUSE": "00",
"AVAILABLE": "187.0000",
"ON_HAND": "189.0000",
"BRAND": "ABC",
"PRICE1": "3.40",
"TAX_CODE1": "-1",
"PROD_CLASS": "DRY"
}
}, {
"Details": {
"CODE": "BAXSAL",
"DESCRIPTION": "SALSA MEXICAN 2LTR [6]",
"LOCATION": "DRY",
"UNITS": "EACH",
"WAREHOUSE": "00",
"AVAILABLE": "439.0000",
"ON_HAND": "445.0000",
"BRAND": "AGE",
"PRICE1": "9.20",
"TAX_CODE1": "-1",
"PROD_CLASS": "DRY"
}
}, {
"Details": {
"CODE": "BAXSEMI",
"DESCRIPTION": "TOMATO SEMI DRIED 2KG [4]",
"LOCATION": "REFRIDGERATOR",
"UNITS": "EACH",
"WAREHOUSE": "00",
"AVAILABLE": "55.0000",
"ON_HAND": "59.0000",
"BRAND": "AGE",
"PRICE1": "21.10",
"TAX_CODE1": "-1",
"PROD_CLASS": "CHILL"
}
}, {
"Details": {
"CODE": "BAXSUN",
"DESCRIPTION": "TOMATO SUN DRIED 2KG [4]",
"LOCATION": "REFRIDGERATOR",
"UNITS": "EACH",
"WAREHOUSE": "00",
"AVAILABLE": "39.0000",
"ON_HAND": "39.0000",
"BRAND": "AGE",
"PRICE1": "15.20",
"TAX_CODE1": "-1",
"PROD_CLASS": "CHILL"
}
}, {
"Details": {
"CODE": "AMFBAM",
"DESCRIPTION": "FLOUR - AM SUP BAKERS 12.5KG",
"LOCATION": "REFRIDGERATOR",
"UNITS": "BAG",
"WAREHOUSE": "00",
"AVAILABLE": "259.0000",
"ON_HAND": "339.0000",
"BRAND": "ALLIE",
"PRICE1": "13.70",
"TAX_CODE1": "-1",
"PROD_CLASS": "CHILL"
}
}, {
"Details": {
"CODE": "ALPKKC",
"DESCRIPTION": "KRANSKY - CHEESE 2.5KG [6]",
"LOCATION": "REFRIDGERATOR",
"UNITS": "EACH",
"WAREHOUSE": "00",
"AVAILABLE": "27.0000",
"ON_HAND": "27.0000",
"BRAND": "ALP",
"PRICE1": "19.30",
"TAX_CODE1": "-1",
"PROD_CLASS": "CHILL"
}
}, {
"Details": {
"CODE": "ABTDP",
"DESCRIPTION": "TOOTHPICK - DOUBLE POINT 1000",
"LOCATION": "DRY",
"UNITS": "EACH",
"WAREHOUSE": "00",
"AVAILABLE": "12.0000",
"ON_HAND": "12.0000",
"BRAND": "ALPEN",
"PRICE1": "1.60",
"TAX_CODE1": "1",
"PROD_CLASS": "DRY"
}
}];
var fullySorted = _.sortBy(profData, obj => obj.Details.PROD_CLASS);
console.log(fullySorted);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
&#13;
使用Lodash:
_.sortBy(profData, 'Details.PROD_CLASS');