Javascript对象与Jquery的结合?

Javascript对象与Jquery的结合?,javascript,jquery,arrays,javascript-objects,Javascript,Jquery,Arrays,Javascript Objects,我在处理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:

我在处理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",          
              }
            ]
    },
  ]
我还有另一组对象,如下所示:

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",          
              }
            ]
    },
  ]
第二个对象中的项目字段将始终是第一个数组中的对象之一

然后我需要得到一个如下所示的对象:

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程序员)比较陌生,我尝试过不同的方法,但都没有用

您可以使用
reduce
将数组分组到一个对象中。使用project和category的串联值作为键。使用
Object.values
将对象转换为数组

var 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”},“some text3”,“startdate”:“2018/06/15”,“结束日期”:“2018/06/16”,“id”:“1”,“只读”:真,“项目”:“项目2”,“类别”:“设计”},{“文本”:“一些文本4”,“开始日期”:“2018/06/13”,“结束日期”:“2018/06/14”,“id”:“1”,“只读”:真,“项目”:“项目2”,“类别”:“范围界定”},{“文本”:“一些文本5”,“开始日期”:“2018/06/14”,“结束日期”:“2018/06/15”,“id”:“1”,“只读”:真,“项目”:“项目3”类别“:”测试“},{”文本“:”一些文本“,”开始日期“:”2018/06/15“,”结束日期“:”2018/06/16“,”id“:”1“,”只读“:”真“,”项目“:”项目3“,”类别“:”构建“}”;
var result=Object.values(ganttEvents.reduce)(c,v)=>{
设k=v.project+“-”+v.category;
c[k]=c[k]|{名称:v.project,描述:v.category,值:[]};
c[k].values.push({to:v.end_date,from:v.start_date,desc:v.text,label:v.text});
返回c;
}, {}));

console.log(结果)
您自己尝试过解决这个问题吗?需求不清楚,到处都缺少很多信息,请您更清楚地解释一下逻辑和需求?@Dean我已经编辑了我的问题,这是逻辑和需求信息更有用吗?嗨@Eddie,谢谢。我刚刚尝试过,但我发现这个错误:
键入错误:Object.values不是函数
@morp\u ByteSense您在哪里使用这个?Nodejs?或angle?比我担心的更糟,这是一个普通的老JavaScript,没有任何框架,它是在一个旧的SharePoint 2010环境中,所以我只有有限的功能。我只限于旧的学校JS和JS库。@morp\u ByteSense I添加了一段代码。不确定它是否适用于您。请让我知道。这太棒了,谢谢!它似乎正在工作,我现在唯一的问题是对象顶层的
名称
部分当前正在返回
null
,我正试图弄清楚这一点。