0

Let's say i have such two objects:

first:

[
    {
        id: "123",
        title: "123",
        options: []
    },
    {
        id: "456",
        title: "456",
        options: [
            {
                id: "0123",
                title: "0123",
                options: []
            }
        ]
    },
    {
        id: "789",
        title: "789",
        options: []
    },
]

and second

[
    {
        id: "123",
        title: "123",
        options: []
    },
    {
        id: "789",
        title: "789",
        options: []
    },
]

as you could see in second array i'm missing this part:

{ id: "456", title: "456", options: [ { id: "0123", title: "0123", options: [] } ] }

how it would be right and better to iterate and find missing elements in angular?

brabertaser19
  • 5,678
  • 16
  • 78
  • 184

2 Answers2

5

You can do it like

<div ng-app>
    <div ng-controller="MyCtrl">{{availableGroups}}
    </div>
</div>

js code

function MyCtrl ($scope) {
    $scope.groups = [
    {
        id: "123",
        title: "123",
        options: []
    },
    {
        id: "456",
        title: "456",
        options: [
            {
                id: "0123",
                title: "0123",
                options: []
            }
        ]
    },
    {
        id: "789",
        title: "789",
        options: []
    },
];

    $scope.assignedGroups = [
    {
        id: "123",
        title: "123",
        options: []
    },
    {
        id: "789",
        title: "789",
        options: []
    },
];


    $scope.availableGroups = (function () {
        var assignedGroupsIds = {};
        var groupsIds = {};
        var result = [];

        $scope.assignedGroups.forEach(function (el, i) {
          assignedGroupsIds[el.id] = $scope.assignedGroups[i];
        });

        $scope.groups.forEach(function (el, i) {
          groupsIds[el.id] = $scope.groups[i];
        });

        for (var i in groupsIds) {
            if (!assignedGroupsIds.hasOwnProperty(i)) {
                result.push(groupsIds[i]);
            }
        }

        return result;    
    }());
}

Here is working jsFiddle

Thanks

sagar43
  • 3,341
  • 3
  • 29
  • 49
1

Let's say that the first array is named first and the second second. Now sort them first:

function comp(a, b){
    if(a.id < b.id) return -1;
    if(a.id > b.id) return 1;
    return 0;
}

first.sort(comp);
second.sort(comp);

Then iterate through them to find missing elements:

var missing = {};
for(var i = 0, j = 0; i < first.length; ++i){
    if(first[i].id == second[j].id){
        j++;
        continue;
    }
    missing.push(first[i]);
}

The missing array now contains objects that is in the first array but not the second one.

Note that I didn't use AngularJS; it's plain Javascript.

David Fang
  • 1,777
  • 1
  • 14
  • 19
  • The time complexity of my solution is O(NlogN + MlogM + N) where N and M are the sizes of arrays. That of the other solution above is O(2*N + M). I'd thought that obj.hasOwnProperty was linear time, however it seems to be O(1) from http://stackoverflow.com/questions/6012242/whats-the-efficiency-in-big-o-notation-of-the-in-operator-or-obj-hasownproper . Sorry for my bad Javascript knowledges. In C++ I would do the way in my answer. – David Fang Jan 09 '15 at 13:35