0

I am trying to loop through an array of objects with multiple dates and other data. I want the dates to be stored in the start and the rest of the data to be stored in an array data[]. In cases the date is similar for any two objects, I want to push all the data(except for the date) into the data[]

Original 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",

  }]

Expected Output:

[{

    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" }]

  }]

I have tried the following code, but it doesnt handle the duplication:

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

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

Comments

1

try this

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);

Comments

1

You can try below logic:

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)


Another way, Let's create array directly:

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);

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.