如何在对象中对数据进行分组,这将导致第二个版本。
web api返回的数据可以更改 - 显然绑定到密钥将不起作用。
以下是现在的格式:
var obj = {
"0": {
"@odata.etag": "W/\"1240107\"",
"statecode_FormattedValue": "Open",
"statecode": 0,
"prioritycode_FormattedValue": "Normal",
"prioritycode": 1,
"activityid": "cb857c42-951e-e711-80d3-00155d0e443f",
"_createdby_value_LogicalName": "systemuser",
"_createdby_value_FormattedValue": "User1",
"_createdby_value": "ea06239e-3918-e711-80d3-00155d0e443f"
},
"1": {
"@odata.etag": "W/\"1240171\"",
"statecode_FormattedValue": "Open",
"statecode": 0,
"prioritycode_FormattedValue": "Normal",
"prioritycode": 1,
"activityid": "374a8d72-4b39-e711-80d5-00155d0e443f",
"_createdby_value_LogicalName": "systemuser",
"_createdby_value_FormattedValue": "User1",
"_createdby_value": "ea06239e-3918-e711-80d3-00155d0e443f"
},
"2": {
"@odata.etag": "W/\"1224371\"",
"statecode_FormattedValue": "Open",
"statecode": 0,
"prioritycode_FormattedValue": "Normal",
"prioritycode": 1,
"activityid": "f31ad69a-5339-e711-80d5-00155d0e443f",
"_createdby_value_LogicalName": "systemuser",
"_createdby_value_FormattedValue": "User1",
"_createdby_value": "ea06239e-3918-e711-80d3-00155d0e443f"
},
"3": {
"@odata.etag": "W/\"1224547\"",
"statecode_FormattedValue": "Open",
"statecode": 0,
"prioritycode_FormattedValue": "Normal",
"prioritycode": 1,
"activityid": "6bee97d8-7939-e711-80d5-00155d0e443f",
"_createdby_value_LogicalName": "systemuser",
"_createdby_value_FormattedValue": "User1",
"_createdby_value": "ea06239e-3918-e711-80d3-00155d0e443f"
}
}
您需要获得的格式:
var obj1 = {
"0": {
"@odata.etag": "W/\"1240107\"",
"statecode": {
"statecode_FormattedValue": "Open",
"statecode": 0,
},
"prioritycode": {
"prioritycode_FormattedValue": "Normal",
"prioritycode": 1,
},
"activityid": "cb857c42-951e-e711-80d3-00155d0e443f",
"_createdby_": {
"_createdby_value_LogicalName": "systemuser",
"_createdby_value_FormattedValue": "User1",
"_createdby_value": "ea06239e-3918-e711-80d3-00155d0e443f"
}
},
"1": {
"@odata.etag": "W/\"1240171\"",
"statecode": {
"statecode_FormattedValue": "Open",
"statecode": 0,
},
"prioritycode": {
"prioritycode_FormattedValue": "Normal",
"prioritycode": 1,
},
"activityid": "374a8d72-4b39-e711-80d5-00155d0e443f",
"_createdby_": {
"_createdby_value_LogicalName": "systemuser",
"_createdby_value_FormattedValue": "User1",
"_createdby_value": "ea06239e-3918-e711-80d3-00155d0e443f"
}
},
"2": {
"@odata.etag": "W/\"1224371\"",
"statecode": {
"statecode_FormattedValue": "Open",
"statecode": 0,
},
"prioritycode": {
"prioritycode_FormattedValue": "Normal",
"prioritycode": 1,
},
"activityid": "f31ad69a-5339-e711-80d5-00155d0e443f",
"_createdby_": {
"_createdby_value_LogicalName": "systemuser",
"_createdby_value_FormattedValue": "User1",
"_createdby_value": "ea06239e-3918-e711-80d3-00155d0e443f"
}
},
"3": {
"@odata.etag": "W/\"1224547\"",
"statecode": {
"statecode_FormattedValue": "Open",
"statecode": 0,
},
"prioritycode": {
"prioritycode_FormattedValue": "Normal",
"prioritycode": 1,
},
"activityid": "6bee97d8-7939-e711-80d5-00155d0e443f",
"_createdby_": {
"_createdby_value_LogicalName": "systemuser",
"_createdby_value_FormattedValue": "User1",
"_createdby_value": "ea06239e-3918-e711-80d3-00155d0e443f"
}
}
}
答案 0 :(得分:1)
您可以为键定义组,并使用组构建新对象。
var object = { 0: { "@odata.etag": "W/\"1240107\"", statecode_FormattedValue: "Open", statecode: 0, prioritycode_FormattedValue: "Normal", prioritycode: 1, activityid: "cb857c42-951e-e711-80d3-00155d0e443f", _createdby_value_LogicalName: "systemuser", _createdby_value_FormattedValue: "User1", _createdby_value: "ea06239e-3918-e711-80d3-00155d0e443f" }, 1: { "@odata.etag": "W/\"1240171\"", statecode_FormattedValue: "Open", statecode: 0, prioritycode_FormattedValue: "Normal", prioritycode: 1, activityid: "374a8d72-4b39-e711-80d5-00155d0e443f", _createdby_value_LogicalName: "systemuser", _createdby_value_FormattedValue: "User1", _createdby_value: "ea06239e-3918-e711-80d3-00155d0e443f" }, 2: { "@odata.etag": "W/\"1224371\"", statecode_FormattedValue: "Open", statecode: 0, prioritycode_FormattedValue: "Normal", prioritycode: 1, activityid: "f31ad69a-5339-e711-80d5-00155d0e443f", _createdby_value_LogicalName: "systemuser", _createdby_value_FormattedValue: "User1", _createdby_value: "ea06239e-3918-e711-80d3-00155d0e443f" }, 3: { "@odata.etag": "W/\"1224547\"", statecode_FormattedValue: "Open", statecode: 0, prioritycode_FormattedValue: "Normal", prioritycode: 1, activityid: "6bee97d8-7939-e711-80d5-00155d0e443f", _createdby_value_LogicalName: "systemuser", _createdby_value_FormattedValue: "User1", _createdby_value: "ea06239e-3918-e711-80d3-00155d0e443f" } },
groups = ['statecode', 'prioritycode', '_createdby_'];
Object.keys(object).forEach(function (k) {
var temp = {};
Object.keys(object[k]).forEach(function (l) {
var group = groups.find(g => l.startsWith(g));
if (group) {
temp[group] = temp[group] || {};
temp[group][l] = object[k][l];
delete object[k][l];
}
});
groups.forEach(g => object[k][g] = temp[g]);
});
console.log(object);

.as-console-wrapper { max-height: 100% !important; top: 0; }