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?

2
  • Use a filter to find arrays where options are not [] Commented Jan 9, 2015 at 13:19
  • @NarekMamikonyan somethink like that: $.each($scope.toDel, function(ind, el) { if (!(el.Id in $scope.articles)) console.log(el); }); Commented Jan 9, 2015 at 13:22

2 Answers 2

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

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

Comments

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.

1 Comment

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 stackoverflow.com/questions/6012242/… . Sorry for my bad Javascript knowledges. In C++ I would do the way in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.