2

I have a scenario like I want to display the contents which are available in only one array, if its present in another array there is no need to display it.

My html is like

<div ng-repeat="array1Value in array1">
    <div ng-repeat="array2Value in array2">
        <div ng-if="isNotFound(array1,array2Value.id)">
            <span>{{array2Value.name}}</span>
        </div>
    </div>
</div>

My js class is like

var app = angular.module("MyApp",{});

app.controller("MyController",function($scope) {
    $scope.array1 = [
        {
            id:"1",name:"one"
        },
        {
            id:"2",name:"two"
        },
        {
            id:"3",name:"three"
        }
    ];

    $scope.array2 = [
        {
            id:"1",name:"one"
        },
        {
            id:"2",name:"two"
        },
        {
            id:"4",name:"four"
        }
    ];

    $scope.alreadyPrinted = [{}];

    $scope.isNotFound = function(array,value){
        for(i = 0; i < array.length; i++){
            if (value === array[i].id) {
                return false;
            }
        }

        if($scope.alreadyPrinted.indexOf(value) > -1){
            return false;
        } else {   
            $scope.alreadyPrinted.push(value);
            return true;
        }
    }
});

I need only four as my output. But as of now nothing is coming. Please help me to correct the issue.

0

3 Answers 3

1

You need to remove external loop... try this:

<div ng-repeat="array2Value in array2">
    <div ng-if="isNotFound(array1,array2Value.id)">
        <span>{{array2Value.name}}</span>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand you right, you want True to be returned if the value is not in the first array.

Try this for your isnotFound function:

$scope.isNotFound = function(array,value){
    for(i = 0; i < array.length; i++){
        if (value !== array[i].id) {
            return true;
        }
    }
    return fase;
}

Or maybe even better:

$scope.isNotFound = function(array,value){
    angular.forEach(array, function(item){
        if (value !== item.id) {
            return true;
        }
    });
    return fase; 
}

I'm not sure why you are repeating over the first array and the allreadyPrinted part. Your html can just be:

<div ng-repeat="array2Value in array2">
    <div ng-if="isNotFound(array1,array2Value.id)">
        <span>{{array2Value.name}}</span>
    </div>
</div>

Comments

0

I think there is problem in your alreadyPrinted code. Your code is displaying 'four' as output. Try to insert one alert box in your code like

for (i = 0; i < array.length; i++) {
    if (value === array[i].id) {
        alert('is not found function called');
        return false;
    }
}

You will see 'four' as output on your browser but for one iteration only. Correct your alreadyPrinted code.alert box will help you to isolate the problem.

Comments

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.