0

Assuming we have 2 arrays:

array1 has 1 event:

var array1 = [
      {
      start: '2018-04-24T10:00:00',
      end: '2018-04-24T11:00:00',
      title: 'Been to Break'
    }
  ];

array2 has 3 events:

  var array2 = [
      {
        start: '2018-04-24T08:00:00',
        end: '2018-04-24T10:00:00',
        title: 'Lunch'
      },
      {
        start: '2018-04-24T10:00:00',
        end: '2018-04-24T11:00:00',
        title: 'Break'
      },
      {
        start: '2018-04-24T13:00:00',
        end: '2018-04-24T14:00:00',
        title: 'Meeting'
      }
    ];

Desired results should be a new array: array3

var array3 = [
  { //event 1
    start: '2018-04-24T08:00:00',
    end: '2018-04-24T10:00:00',
    title: 'Lunch'
  },
  { //event 2
    start: '2018-04-24T10:00:00',
    end: '2018-04-24T11:00:00',
    title: 'Been to Break'
  },
  { //event 3
    start: '2018-04-24T13:00:00',
    end: '2018-04-24T14:00:00',
    title: 'Meeting'
  }
];

You can see from the desired results in array3 that the event 2 in array2 was replaced with the event 1 in array1 since the start and end values are a match.

Can this be done in JavaScript? and what about performance if we have let's say 1000 events to loop through

Thanks

7
  • Is the array sorted by date? Will this search need to be done frequently? Commented Apr 24, 2018 at 22:34
  • @CrazyTrain in this example yes, but I have a case where it is not Commented Apr 24, 2018 at 22:36
  • If it was sorted and you kept it sorted, you could perform a binary search for much greater performance than a linear search. If this operation will happen somewhat frequently, it may be worth maintaining a sorted ordering, if possible. Commented Apr 24, 2018 at 22:38
  • @CrazyTrain just realised the events are pulled from db then they can be sorted prior to assigning them to the array. Is there a workaround to get array3 as per the example. cheers Commented Apr 24, 2018 at 22:41
  • Where did 'Breaky' come from? Commented Apr 24, 2018 at 22:41

1 Answer 1

1

A simplistic approach would be to loop over the update array (array1) to find a match in array2 and replace it if found.

var array1 = [{
  start: '2018-04-24T10:00:00',
  end: '2018-04-24T11:00:00',
  title: 'Been to Break'
}];

var array2 = [{
    start: '2018-04-24T08:00:00',
    end: '2018-04-24T10:00:00',
    title: 'Lunch'
  },
  {
    start: '2018-04-24T10:00:00',
    end: '2018-04-24T11:00:00',
    title: 'Break'
  },
  {
    start: '2018-04-24T13:00:00',
    end: '2018-04-24T14:00:00',
    title: 'Meeting'
  }
];

array1.forEach((evUpdate) => {
  const matchIndex = array2.findIndex((ev) => ev.start === evUpdate.start && ev.end === evUpdate.end);

  if (matchIndex > -1) {
    array2.splice(matchIndex, 1, evUpdate);
  }
});

console.log(array2);

As per performance, there are a lot of other factors that are unknown. For instance, if this is a runtime operation that happens often but the underlying data is fairly static then why not create and store a resultant in a data store?

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

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.