gpt4 book ai didi

javascript - 对象 JS 中的 groupBy

转载 作者:行者123 更新时间:2023-12-03 03:21:14 24 4
gpt4 key购买 nike

如何对对象中的数据进行分组,这将产生第二个版本。

Web api 返回的数据可以更改 - 显然绑定(bind)到 key 将不起作用。

以下是现在的格式:

    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"
}
}

}

最佳答案

您可以定义键的组并使用组构建新对象。

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; }

关于javascript - 对象 JS 中的 groupBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46567911/

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