如何遍历对象数组并将键值对添加到基于重复条目的另一个对象数组中

时间:2019-08-22 13:09:01

标签: javascript arrays

我试图遍历具有多个日期和其他数据的对象数组。我希望将日期存储在开始位置,并将其余数据存储在数组data []中。如果任何两个对象的日期相似,我想将所有数据(日期除外)推入data []

原始数据:

[{

    start: "2019-08-23",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Lecture",

  },

  {

    start: "2019-08-25",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Tutorial",

  },

  {

    start: "2019-08-25",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Lecture",

  }]

预期输出:

[{

    start: "2019-08-23",

    data: [{ title: "Week 5 Dispensing Tutorial 3 Quiz", type: "Lecture" }]

  },

  {

    start: "2019-08-25",

    data: [{ title: "Week 5 Dispensing Tutorial 3 Quiz", type: "Tutorial" }, {title: "Week 5 Dispensing Tutorial 3 Quiz", type: "Lecture" }]

  }]

我尝试了以下代码,但无法处理重复代码:

var result[] = ITEMS.map(ITEMS => ({
      start: ITEMS.start,
      data: [
        {
          title: ITEMS.title,
          type: ITEMS.type
        }
      ]
    }));

3 个答案:

答案 0 :(得分:2)

const input = [{

    start: "2019-08-23",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Lecture",

  },

  {

    start: "2019-08-25",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Tutorial",

  },

  {

    start: "2019-08-25",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Lecture",

  }];

  const output = [];
  input.forEach(v => {
      if (!output.map(o => o.start).includes(v.start)) { //If there are no duplicates
          output.push({
              start: v.start,
              data: [{
                   title: v.title,
                   type: v.type
              }]
          });
      }
      else { //If there are duplicates
          for (let i = 0; i < output.length; i++) {
              if (output[i].start === v.start) { //Find the duplicated one
                  output[i].data.push({
                      title: v.title,
                      type: v.type
                  });
                  break;
              }
          }
      }
  });

  console.log(output);

答案 1 :(得分:1)

尝试

var arrray =[{

    start: "2019-08-23",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Lecture",

  },

  {

    start: "2019-08-25",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Tutorial",

  },

  {

    start: "2019-08-25",

    title: "Week 5 Dispensing Tutorial 3 Quiz ",

    type: "Lecture",

  }];
  
  let newArrray =[];
  
  arrray.forEach(function(element)
  {
    let dOjb = newArrray.find(x => x.start === element.start);
    if(dOjb)
    {
      dOjb.data.push({
          'title': element.title,
          'type': element.type
        })
    }
    else
    {
      newArrray.push(
      {
        'start': element.start,
        data: [
        {
          'title': element.title,
          'type': element.type
        }
      ]}
      );
    }

});
  
  console.log(newArrray);

答案 2 :(得分:1)

您可以尝试以下逻辑:

var data = [{ start: "2019-08-23", title: "Week 5 Dispensing Tutorial 3 Quiz ", type: "Lecture", }, { start: "2019-08-25", title: "Week 5 Dispensing Tutorial 3 Quiz ", type: "Tutorial", }, { start: "2019-08-25", title: "Week 5 Dispensing Tutorial 3 Quiz ", type: "Lecture", }]

var nData = {};

(data || []).forEach( e => {
  nData[e.start] = {
    data : (nData[e.start] ? nData[e.start].data.push({title: e.title, type: e.type}) && nData[e.start].data : [{title: e.title, type: e.type}])
  }
});

var modifiedData = Object.keys(nData).map(k => {
  return {
    start: k,
    data: nData[k].data 
  }
})

console.log(modifiedData)


另一种方法,让我们直接创建array

var data = [{ start: "2019-08-23", title: "Week 5 Dispensing Tutorial 3 Quiz ", type: "Lecture", }, { start: "2019-08-25", title: "Week 5 Dispensing Tutorial 3 Quiz ", type: "Tutorial", }, { start: "2019-08-25", title: "Week 5 Dispensing Tutorial 3 Quiz ", type: "Lecture", }]

var nData = [];

(data || []).forEach( e => {
  var i = (index = nData.findIndex(d => d.start === e.start)) >=0 ? index : nData.length;
  nData[i] = {
    start: e.start,
    data : (nData[i] && nData[i].data ? nData[i].data.push({title: e.title, type: e.type}) && nData[i].data : [{title: e.title, type: e.type}])
  }
});

console.log(nData);

希望这会有所帮助!