Compare two arrays and add non-repeated values to a new array in Angular js

1

I'm trying to compare two arrays with different lengths and that the non-repeating values are added to a new array.

I would like to know how I do it, then I share my code:

var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope, $rootScope) { 
        $rootScope.res=[];
    $scope.Array1 = [
  {
    "responsable": "S/N",
    "tipo": "S/N",
    "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
    "nivel": "PREGRADO",
    "fecha_clase": "05-04-2016"
  },
  {
    "responsable": "S/N",
    "tipo": "S/N",
    "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
    "nivel": "PREGRADO",
    "fecha_clase": "12-04-2016"
  },
  {
    "responsable": "S/N",
    "tipo": "S/N",
    "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
    "nivel": "PREGRADO",
    "fecha_clase": "19-04-2016"
  },
  {
    "responsable": "S/N",
    "tipo": "S/N",
    "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
    "nivel": "PREGRADO",
    "fecha_clase": "26-04-2016"
  },
  {
    "responsable": "S/N",
    "tipo": "S/N",
    "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
    "nivel": "PREGRADO",
    "fecha_clase": "03-05-2016"
  },
  {
    "responsable": "S/N",
    "tipo": "S/N",
    "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
    "nivel": "PREGRADO",
    "fecha_clase": "10-05-2016"
  },
  {
    "responsable": "S/N",
    "tipo": "S/N",
    "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
    "nivel": "PREGRADO",
    "fecha_clase": "17-05-2016"
  }
];
$scope.Array2=[{"fecha":"05-04-2016"},{"fecha":"12-04-2016"},{"fecha":"19-04-2016"},{"fecha":"26-04-2016"},{"fecha":"03-05-2016"},{"fecha":"10-05-2016"},{"fecha":"17-05-2016"},{"fecha":"24-05-2016"},{"fecha":"31-05-2016"},{"fecha":"07-06-2016"},{"fecha":"14-06-2016"},{"fecha":"21-06-2016"},{"fecha":"28-06-2016"},{"fecha":"05-07-2016"},{"fecha":"12-07-2016"},{"fecha":"19-07-2016"},{"fecha":"26-07-2016"},{"fecha":"02-08-2016"}];

for(var a=0;a<$scope.Array2.length;a++){
  for(var b=0;b<$scope.Array1.length;b++){
    var fechasLista=$scope.Array1[b].fecha_clase;
    var fechaConvertida =$scope.Array2[a].fecha;
    if(fechasLista != fechaConvertida){
    //aqui es donde deseo que los valores que no estan repetidos se agreguen en $rootScope.res
     $rootScope.res.push({r:'no repetidos '+fechaConvertida});
    }
  }
}
}    

finally I present them in the following way:

<div ng-controller="MyCtrl">
  <div ng-repeat="r in res">
    {{r.r}}
  </div>
</div>

I hope you can help me. Thank you in advance

    
asked by Dimoreno 09.07.2016 в 07:49
source

4 answers

1

You can get it without using any libraries with Array.prototype.filter :

const uniqueArray = array1.filter(value => array2.indexOf(value) == -1)
    
answered by 08.08.2016 в 14:11
0

This problem goes beyond angular, since it is a problem of general programming solved by javascript.

One solution is to use the library lodash and its method uniqBy , so that it filters the unwanted elements based on a function that you indicate, leaving only the only ones.

Personally, I always use the lodash library because it adds many functions that do not come by default in JS and helps make the code much more readable for the following devs.

    
answered by 09.07.2016 в 12:46
0

Your question basically is: How can I generate a new Array of objects, based on 2 arrays of objects?

You defined your first object, say courseCourse with: responsible, type, guid, level and class_date .

Your second object, let's say DateDate only with date .

And you want an array of Date objects, where NO there are dateDate.date equal toCourse object.class_date

You can filter the array that matters to you; array2.filter (dateObject) with the searched condition; that NO ! must be in array1. There are other ways to do it, I think I like this one:

let arrayFiltrado = array2.filter(objetoFecha =>
  !array1.some(objetoCurso =>
    objetoCurso.fecha_clase == objetoFecha.fecha
  )
)
    
answered by 08.04.2017 в 15:14
0

Just apply filter to apply a filtering of non-repeated objects. Additionally you can use some for a quick check if you have or not an element with a repeated date in the second array.

var myApp = angular.module('myApp', []);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

myApp.controller('MyCtrl', function($scope, $rootScope) {
  $scope.array1 = [{
      "responsable": "S/N",
      "tipo": "S/N",
      "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
      "nivel": "PREGRADO",
      "fecha_clase": "05-04-2016"
    },
    {
      "responsable": "S/N",
      "tipo": "S/N",
      "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
      "nivel": "PREGRADO",
      "fecha_clase": "12-04-2016"
    },
    {
      "responsable": "S/N",
      "tipo": "S/N",
      "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
      "nivel": "PREGRADO",
      "fecha_clase": "19-04-2016"
    },
    {
      "responsable": "S/N",
      "tipo": "S/N",
      "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
      "nivel": "PREGRADO",
      "fecha_clase": "26-04-2016"
    },
    {
      "responsable": "S/N",
      "tipo": "S/N",
      "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
      "nivel": "PREGRADO",
      "fecha_clase": "03-05-2016"
    },
    {
      "responsable": "S/N",
      "tipo": "S/N",
      "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
      "nivel": "PREGRADO",
      "fecha_clase": "10-05-2016"
    },
    {
      "responsable": "S/N",
      "tipo": "S/N",
      "guid": "a7c2dd58-eb27-004e-e043-ac10360d004e",
      "nivel": "PREGRADO",
      "fecha_clase": "18-05-2016"
    }
  ];

  $scope.array2 = [{
    "fecha": "05-04-2016"
  }, {
    "fecha": "12-04-2016"
  }, {
    "fecha": "19-04-2016"
  }, {
    "fecha": "26-04-2016"
  }, {
    "fecha": "03-05-2016"
  }, {
    "fecha": "10-05-2016"
  }, {
    "fecha": "17-05-2016"
  }, {
    "fecha": "24-05-2016"
  }, {
    "fecha": "31-05-2016"
  }, {
    "fecha": "07-06-2016"
  }, {
    "fecha": "14-06-2016"
  }, {
    "fecha": "21-06-2016"
  }, {
    "fecha": "28-06-2016"
  }, {
    "fecha": "05-07-2016"
  }, {
    "fecha": "12-07-2016"
  }, {
    "fecha": "19-07-2016"
  }, {
    "fecha": "26-07-2016"
  }, {
    "fecha": "02-08-2016"
  }];

  $scope.res = $scope.array1.filter(obj => {
    const exists = $scope.array2.some(obj2 => (
      obj2.fecha === obj.fecha_clase
    ));
    
    if (!exists) {
      return obj;
    }
  });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller="MyCtrl" style="padding: 25px">
    <table class="table table-striped table-bordered">
      <thead>
        <tr>
          <th>Responsable</th>
          <th>Tipo</th>
          <th>GUID</th>
          <th>Nivel</th>
          <th>Fecha</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="r in res">
          <td>{{ r.responsable }}</td>
          <td>{{ r.tipo }}</td>
          <td>{{ r.guid }}</td>
          <td>{{ r.nivel }}</td>
          <td>{{ r.fecha_clase }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
    
answered by 08.04.2017 в 15:49