JavaScript数据处理(对象数组操作生成新数组)
本帖最后由 a1chemy 于 2022-9-9 08:15 编辑我的表单提交的原数据格式是这样的:
{
"task_number": "开发测试大任务编号",
"task_name": "开发测试大任务名称",
"task_desc": "开发测试任务说明",
"task_node": [
{
"id": 0,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"task_id": 0,
"node_code": "1-2-1",
"node_name": "测试子任务编号",
"node_start": "2022-10-01 08:00:00",
"node_end": "2022-10-15 08:00:00",
"node_num": 2,
"node_subject": "乡镇纪委领导班子",
"node_desc": "暂无",
"node_demand": "测试子任务编号",
"node_attach_num": 3,
"time": [
"2022-10-01T00:00:00.000Z",
"2022-10-15T00:00:00.000Z"
],
"task_filling": [
"{\"full_name\":\"张飞\",\"user_id\":20}",
"{\"full_name\":\"王林\",\"user_id\":26}"
]
},
{
"id": 0,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"task_id": 0,
"node_code": "1-2-1",
"node_name": "测试子任务编号",
"node_start": "2022-10-01 08:00:00",
"node_end": "2022-10-15 08:00:00",
"node_num": 2,
"node_subject": "乡镇纪委领导班子",
"node_desc": "暂无",
"node_demand": "测试子任务编号",
"node_attach_num": 3,
"time": [
"2022-10-01T00:00:00.000Z",
"2022-10-15T00:00:00.000Z"
],
"task_filling": [
"{\"full_name\":\"韩信\",\"user_id\":22}",
"{\"full_name\":\"李白\",\"user_id\":21}"
]
}
]
}
现在后端需要的格式是这样的:
{
"task_number": "开发测试大任务编号",
"task_name": "开发测试大任务名称",
"task_desc": "开发测试任务说明",
"task_filling": [
"{\"full_name\":\"张飞\",\"user_id\":20}",
"{\"full_name\":\"王林\",\"user_id\":26}"
]
"task_node": [
{
"id": 0,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"task_id": 0,
"node_code": "1-2-1",
"node_name": "测试子任务编号",
"node_start": "2022-10-01 08:00:00",
"node_end": "2022-10-15 08:00:00",
"node_num": 2,
"node_subject": "乡镇纪委领导班子",
"node_desc": "暂无",
"node_demand": "测试子任务编号",
"node_attach_num": 3,
"time": [
"2022-10-01T00:00:00.000Z",
"2022-10-15T00:00:00.000Z"
],
},
]
},
{
"task_number": "开发测试大任务编号",
"task_name": "开发测试大任务名称",
"task_desc": "开发测试任务说明",
"task_filling": [
"{\"full_name\":\"韩信\",\"user_id\":22}",
"{\"full_name\":\"李白\",\"user_id\":21}"
]
"task_node": [
{
"id": 0,
"created_at": null,
"updated_at": null,
"deleted_at": null,
"task_id": 0,
"node_code": "1-2-1",
"node_name": "测试子任务编号",
"node_start": "2022-10-01 08:00:00",
"node_end": "2022-10-15 08:00:00",
"node_num": 2,
"node_subject": "乡镇纪委领导班子",
"node_desc": "暂无",
"node_demand": "测试子任务编号",
"node_attach_num": 3,
"time": [
"2022-10-01T00:00:00.000Z",
"2022-10-15T00:00:00.000Z"
],
},
]
}
就是要把task_node里的task_filling放到外面,根据task_node下的数量生成对应数量的新数组。还请各位大佬帮帮忙,非常感谢! <script>
var obj = {
task_number: "开发测试大任务编号",
task_name: "开发测试大任务名称",
task_desc: "开发测试任务说明",
task_node: [
{
id: 0,
created_at: null,
updated_at: null,
deleted_at: null,
task_id: 0,
node_code: "1-2-1",
node_name: "测试子任务编号",
node_start: "2022-10-01 08:00:00",
node_end: "2022-10-15 08:00:00",
node_num: 2,
node_subject: "乡镇纪委领导班子",
node_desc: "暂无",
node_demand: "测试子任务编号",
node_attach_num: 3,
time: ["2022-10-01T00:00:00.000Z", "2022-10-15T00:00:00.000Z"],
task_filling: [
'{"full_name":"张飞","user_id":20}',
'{"full_name":"王林","user_id":26}',
],
},
{
id: 0,
created_at: null,
updated_at: null,
deleted_at: null,
task_id: 0,
node_code: "1-2-1",
node_name: "测试子任务编号",
node_start: "2022-10-01 08:00:00",
node_end: "2022-10-15 08:00:00",
node_num: 2,
node_subject: "乡镇纪委领导班子",
node_desc: "暂无",
node_demand: "测试子任务编号",
node_attach_num: 3,
time: ["2022-10-01T00:00:00.000Z", "2022-10-15T00:00:00.000Z"],
task_filling: [
'{"full_name":"韩信","user_id":22}',
'{"full_name":"李白","user_id":21}',
],
},
],
};
var arr = [];
(function () {
obj.task_node.forEach((item, index) => {
let obj3 = {};
obj3.task_number = obj.task_number;
obj3.task_name = obj.task_name;
obj3.task_desc = obj.task_desc;
obj3.task_filling = item.task_filling;
obj3.task_node = item;
delete obj3.task_node.task_filling;
arr.push(obj3);
});
})();
console.log(arr);
</script>
页:
[1]