-1

An easy question from a noob.

I have this example where I create a list with a Key/Value structure, I use the Id as the Key and the rest of the content is the value so if I add more records they will be added to my list $scope.itemList [ ]

var data = [
              {
                id: 1,
                name : "Peter",
                lastname : "Smith",
                age : 36
              }, {
                id: 2,
                name : "Peter",
                lastname : "Smith",
                age : 36
              }
            ];

            $scope.itemList = [];
            angular.forEach(data, function(item){
                var obj = {};
                var valObj = {};

                valObj.name = item.name;
                valObj.lastname = item.lastname;
                valObj.age = item.age;

                obj[item.id] = valObj;
                $scope.itemList.push(obj);
            });

The thing is that I could have more records in my json with the same id and I want to avoid adding duplicated records, now this code will push everything it comes in it without matter if the id's are duplicated, but I dont know how to compare the var obj {} with the item.id, I tried different "options" but none of them work.

Some help on this will be great and I'll appreciate it a lot

kennechu
  • 1,412
  • 9
  • 25
  • 37

2 Answers2

1

Solution

var data = [
  {
    id: 1,
    name : "Peter1",
    lastname : "Smith",
    age : 36
  }, {
    id: 2,
    name : "Peter2",
    lastname : "Smith",
    age : 36
  }, {
    id: 2,
    name : "Peter3",
    lastname : "Smith",
    age : 36
  }
];

$scope.itemList = [];
angular.forEach(data, function(item){
  var obj = {};
  var valObj = {};

  valObj.name = item.name;
  valObj.lastname = item.lastname;
  valObj.age = item.age;

  if ( !obj[item.id]) {
    obj[item.id] = valObj;
    $scope.itemList.push(obj);
  }

});

however if you dont have a very good reason for obj[item.id] then you should be looking at findIndex of lodash or something like that.

I mean i cant see a reason why you would have to have 2 separate objects like that.

update

var data = [
  {
    id: 1,
    name : "Peter1",
    lastname : "Smith",
    age : 36
  }, {
    id: 2,
    name : "Peter2",
    lastname : "Smith",
    age : 36
  }, {
    id: 2,
    name : "Peter3",
    lastname : "Smith",
    age : 36
  }
];

$scope.itemsList = [];
angular.forEach(data, function(item){
  if(_.findIndex($scope.itemsList, function(itemList){ return item.id === itemList.id }) === -1){
    $scope.itemsList.push(item);
  }
});
Neta Meta
  • 4,001
  • 9
  • 42
  • 67
  • Thanks a lot for your time.. So what is a better option to avoid using two objects, I mean, I made it this way because it works for me, I got the expected result but if there's a way to optimize it I open because Im learning =) – kennechu May 24 '16 at 02:07
  • Check the update, - using lodash, you can easily make a function that replaces _.findIndex – Neta Meta May 24 '16 at 02:17
1

If you changed the itemList from a list to a dictionary, I think it would do what you need. Something like:

var data = [
              {
                id: 1,
                name : "Peter",
                lastname : "Smith",
                age : 36
              }, {
                id: 2,
                name : "Peter",
                lastname : "Smith",
                age : 36
              }
            ];

            $scope.itemDictionary = {};
            angular.forEach(data, function(item){
                var obj = {};
                var valObj = {};

                valObj.name = item.name;
                valObj.lastname = item.lastname;
                valObj.age = item.age;

                obj[item.id] = valObj;
                if(!$scope.itemDictionary[item.id]){
                     $scope.itemDictionary[item.id] = obj;
                }
            });
poppertech
  • 1,286
  • 2
  • 9
  • 17
  • Hi bwyn, I wich moment in this code you "push" the data, it works, I already test it because I log the result, but I dont get when this happen. – kennechu May 24 '16 at 06:01