0

I have an array with objects in it that looks like this:

object

I need to check to see if any of the start dates are the same, and if they are, do something with those objects. So for example, if arr[0], arr[3], arr[4] all have a start date of Mon Apr 25 2016 16:00:00 GMT-0700 (Pacific Daylight Time), then I will need to select those 3 objects and do something with them.

I'm not sure how to do this, any help is appreciated.

2
  • 2
    can you post the actual object? and what have you tried? Commented Apr 21, 2016 at 23:40
  • you can use filter to extract the element in the array with a exactly date array.filter((e) => {return e.start === "Mon Apr 25 2016 16:00:00 GMT-0700 (Pacific Daylight Time)"}); Commented Apr 21, 2016 at 23:51

3 Answers 3

2

You can transform your list of objects to be mapped by their start attribute and check to see if any of those dates contain more than one object.

Here's your helper:

function transform(list) {
  var map = {};
  for (var i = 0; i < list.length; i++) {
    var object = list[i];
    if (object.start in map) {
      map[object.start].push(object);
    } else {
      map[object.start] = [];
      map[object.start].push(object);
    }
  }
  return object;
}

Then all you need to do is call transform with your list of objects to get back your map of objects identified by their start dates. Iterate through them and you'll get all of the objects for each start date.

var startDates = transform(list); // your list of objects mapped by the start date
// loop through each start date
for (var startDate in startDates) {
  var listForStartDate = startDates[startDate];

  // check to see if more than one object exists for this start date
  if (listForStartDate.length > 1) {
    // do whatever you want with your list of objects for the current start date
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

First loop through the array, then loop again from that point on, check to see if any of the starts are the same...

for (var i = 0; i < array.length; i++) {
  var currentObj = array[i];
  for (var j = i + 1; j < array.length; j++) {
    var other = array[j];
    if (currentObj.start === other.start) {
      // do stuff here
    }
  }
}

Comments

0

I would first use something like Lodash's _.groupBy() to create an object where the key is the date (as a formatted string or string of the numeric value) and the value is an array of objects with the same date.

Then iterate over the object and if the value array has more than one element, do something with it.

e.g. if using Lodash (assuming your array of objects is called array):

var grouped = _.groupBy(array, function (obj) { return '' + obj.start.getTime(); });
_.each(grouped, function (objects, date) {
    if (objects.length > 1) {
        doSomething(objects);
    }
});

If not using Lodash/Underscore, you can just roll your own grouping function:

function groupBy(collection, callback) {
    var output = {};
    angular.forEach(collection, function (obj) {
        var groupKey = callback(obj);
        if (!output[groupKey]) {
            output[groupKey] = [obj];
        } else {
            output[groupKey].push(obj);
        }
    });
    return output;
}

// to use it:

var groupedByDate = groupBy(array, function (obj) { return '' + obj.start.getTime(); });
angular.forEach(groupedByDate, function (objects, date) {
    if (objects.length > 1) {
        doSomething(objects);
    }
});

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.