1

Data shouldn't duplicate or it should remove the duplicate data.

Here's the code:

dateList = [
 [{
   date: "2019-12-12 03:00:00"
 },{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-13 03:16:14"
 },{
   date: "2019-12-11 03:16:14"
 }],
[{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-13 03:16:14"
 },{
   date: "2019-12-11 03:16:14"
 }],
[{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-13 03:16:14"
 },{
   date: "2019-12-17 03:16:14"
 }],
[{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-12 03:16:14"
 },{
   date: "2019-12-13 03:16:14"
 },{
   date: "2019-12-15 03:16:14"
 }],

]

dateList.map((x: any) => {
          x.filter((item: any) => {
            console.log(item);

          });
        });

How to combine or merge without duplicating or remove the same date data based on the date in angular.

Example output:

[
    {
      date: "2019-12-12 03:00:00"
    },
    {
      date: "2019-12-13 03:16:14"
    },
    {
      date: "2019-12-17 03:16:14"
    },
    {
      date: "2019-12-15 03:16:14"
    }
  ]

The same date should be removed and if the data not the same it will stay.

1
  • I think you need to do a .reduce() instead of a .map() Commented Jan 22, 2020 at 2:50

1 Answer 1

1

Used plain JavaScript to achieve your requirement

                var dateList = [
                [{
                date: "2019-12-12 03:00:00"
                },{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-13 03:16:14"
                },{
                date: "2019-12-11 03:16:14"
                }],
                [{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-13 03:16:14"
                },{
                date: "2019-12-11 03:16:14"
                }],
                [{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-13 03:16:14"
                },{
                date: "2019-12-17 03:16:14"
                }],
                [{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-12 03:16:14"
                },{
                date: "2019-12-13 03:16:14"
                },{
                date: "2019-12-15 03:16:14"
                }]
                ]

                Array.prototype.contains = function(v) {
                    for (var i = 0; i < this.length; i++) {
                    if (this[i].date === v.date) return true;
                    }
                    return false;
                };
                
                Array.prototype.unique = function() {
                    var arr = [];
                    for (var i = 0; i < this.length; i++) {
                    if (!arr.contains(this[i])) {
                        arr.push(this[i]);
                    }
                    }
                    return arr;
                }

                function myFunction() {
                    var merged = [].concat.apply([], dateList);
                    var uniques = merged.unique();
                    
                    
                    console.log(uniques);
                }
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click for answer</button>

</body>
</html>

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

2 Comments

can you create on stackblitz?
stackblitz.com/edit/js-z5lusp?file=index.html accept the answer if you find it useful

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.