【发布时间】:2018-11-22 18:23:09
【问题描述】:
我在处理 Javascript/jQuery 中的数据时遇到问题,我需要一些帮助。
我有一个像这样的对象数组:
var projects = [
{title:'project1'},
{title:'project2'},
{title:'project3'},
];
我有另一个对象数组,如下所示:
ganttEvents = [
{
text: 'some text',
start_date: '2018/06/13',
end_date: '2018/06/14',
id: '1',
readonly: true,
project: 'project1',
category: 'scoping',
}
{
text: 'some text2',
start_date: '2018/06/14',
end_date: '2018/06/15',
id: '1',
readonly: true,
project: 'project2',
category: 'scoping',
}
{
text: 'some text3',
start_date: '2018/06/15',
end_date: '2018/06/16',
id: '1',
readonly: true,
project: 'project2',
category: 'design',
}
{
text: 'some text4',
start_date: '2018/06/13',
end_date: '2018/06/14',
id: '1',
readonly: true,
project: 'project2',
category: 'scoping',
}
{
text: 'some text5',
start_date: '2018/06/14',
end_date: '2018/06/15',
id: '1',
readonly: true,
project: 'project3',
category: 'testing',
}
{
text: 'some text6',
start_date: '2018/06/15',
end_date: '2018/06/16',
id: '1',
readonly: true,
project: 'project3',
category: 'build',
}
];
第二个对象中的项目字段将始终是第一个数组中的一个对象。
然后我需要得到一个看起来像这样的对象:
source: [
{
name: "project1", // a project defined in the projects array
desc: "scoping", // the category from the ganttEvents array of objects
values: [
{
to: "2018/06/13", // the start_date from the ganttEvents array of objects
from: "2018/06/14", // the end_date from the ganttEvents array of objects
desc: "some text", // the text from the ganttEvents array of objects
label: "some text", // the text from the ganttEvents array of objects
}
]
},
{
name: "project2", // a project defined in the projects array
desc: "scoping", // the category from the ganttEvents array of objects
values: [
{
to: "2018/06/14",
from: "2018/06/15",
desc: "some text2",
label: "some text2",
},
{
to: "2018/06/13",
from: "2018/06/14",
desc: "some text4",
label: "some text4",
},
]
},
{
name: "project3", // a project defined in the projects array
desc: "testing", // the category from the ganttEvents array of objects
values: [
{
to: "2018/06/14",
from: "2018/06/15",
desc: "some text5",
label: "some text5",
}
]
},
{
name: "project3", // a project defined in the projects array
desc: "build", // the category from the ganttEvents array of objects
values: [
{
to: "2018/06/15",
from: "2018/06/16",
desc: "some text6",
label: "some text6",
}
]
},
]
每个项目的所有阶段都可能有多个值,并且可能有根本没有事件的项目需要从源对象中省略。
你能帮忙吗?
编辑:
这背后的背景是我正在使用 SharePointPlus 从 SharePoint 列表中提取事件列表。这会产生 ganttEvents 数组。我需要将它插入到 jQuery.Gantt 库中,该库需要以特定方式格式化事件。
我很抱歉,但我对 Javascript(通常是 Python 程序员)比较陌生,我尝试了不同的方法来做这件事,但无济于事。
【问题讨论】:
-
你有没有尝试过自己解决这个问题?
-
不清楚的需求,很多信息在这里和那里丢失,可以解释清楚逻辑和需求吗?
-
@Dean 我已经编辑了我的问题,这是逻辑和需求信息更有用吗?
标签: javascript jquery arrays javascript-objects