ABC
ABC

Reputation: 832

How to merge without duplicating data in angular

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.

Upvotes: 1

Views: 57

Answers (1)

Uthistran Selvaraj
Uthistran Selvaraj

Reputation: 1371

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>

Upvotes: 1

Related Questions