javascript分组对象数组

时间:2016-12-05 09:49:23

标签: javascript

我有一个对象数组

[{
  quesName: 'ques1',
  ans: 'ans1',
  sectionName: 'section1'
}, {
  quesName: 'ques2',
  ans: 'ans2',
  sectionName: 'section1'
}, {
  quesName: 'ques3',
  ans: 'ans3',
  sectionName: 'section2'
}, {
  quesName: 'ques4',
  ans: 'ans4',
  sectionName: 'section2'
}, {
  quesName: 'ques5',
  ans: 'ans5',
  sectionName: 'section3'
}, {
  quesName: 'ques6',
  ans: 'ans6',
  sectionName: 'section3'
}]

我想将这些分组到像

这样的对象中
[{
  sectionName: "section1",
  sectionValues: [{
    quesName: 'ques1',
    ans: 'ans1'
  }, {
    quesName: 'ques2',
    ans: 'ans2'
  }]
}, {
  sectionName: "section2",
  sectionValues: [{
    quesName: 'ques3',
    ans: 'ans3'
  }, {
    quesName: 'ques4',
    ans: 'ans4'
  }]
}, {
  sectionName: "section2",
  sectionValues: [{
    quesName: 'ques5',
    ans: 'ans5'
  }, {
    quesName: 'ques6',
    ans: 'ans6'
  }]
}]

0 个答案:

没有答案