-3
const timings = [
  {startH: 10, startM: 20, endH: 11, endM: 30},
  {startH: 10, startM: 20, endH: 11, endM: 30},
  {startH: 9, startM: 10, endH: 11, endM: 30},
  {startH: 10, startM: 20, endH: 10, endM: 30},
  {startH: 10, startM: 20, endH: 11, endM: 30},
  {startH: 10, startM: 20, endH: 11, endM: 30}
];

return all equal objects inside the array

ashwin1014
  • 351
  • 1
  • 5
  • 18
  • Hi! Please have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I also recommend Jon Skeet's [Writing the Perfect Question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and [Question Checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). At a minimum, we need to know what your definition of "similar" is, and we need to see your attempt to solve the problem and a description of specifically where you're having trouble with it. – T.J. Crowder Nov 01 '19 at 12:20
  • Your update solves the "similar" problem, but again, your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Nov 01 '19 at 12:26
  • Please also [search](/search?q=%5Bjs%5D+remove+identical+object+from+array) before posting, more about searching [here](/help/searching). I suspect this is a duplicate of https://stackoverflow.com/questions/36909866/remove-similar-objects-from-array, which should already have an answer for you. – T.J. Crowder Nov 01 '19 at 12:28

1 Answers1

1

Try to use next code:

const timings = [
  {startH: 10, startM: 20, endH: 11, endM: 30},
  {startH: 10, startM: 20, endH: 11, endM: 30},
  {startH: 9, startM: 10, endH: 11, endM: 30},
  {startH: 10, startM: 20, endH: 10, endM: 30},
  {startH: 10, startM: 20, endH: 11, endM: 30},
  {startH: 10, startM: 20, endH: 11, endM: 30}
];
Array.prototype.uniqueObjects = function (props) {
    function compare(a, b) {
      var prop;
        if (props) {
            for (var j = 0; j < props.length; j++) {
              prop = props[j];
                if (a[prop] != b[prop]) {
                    return false;
                }
            }
        } else {
            for (prop in a) {
                if (a[prop] != b[prop]) {
                    return false;
                }
            }

        }
        return true;
    }
    return this.filter(function (item, index, list) {
        for (var i = 0; i < index; i++) {
            if (compare(item, list[i])) {
                return false;
            }
        }
        return true;
    });
};
 
var uniqueObject = timings.uniqueObjects(["startH", "startM", "endH", "endM"]);

console.log(uniqueObject);
Aksen P
  • 4,564
  • 3
  • 14
  • 27