1

I have the below model in JS . I am using angular js

$scope.data = {
            FocusOn: " ",
            Filters: [],
            Range: {
                From: "",
                To: ""
            }
        }

I have the below function :

$scope. addField = function ($type, $value) {
            $scope.data1 = {
                FilterName: $type,
                FilterValue: $value
            };
            if ($scope.data.Filters[$type] === undefined) {
                $scope.data.Filters.push($scope.data1);
            }
            $scope.data1 = "";
            $scope.Json = angular.toJson($scope.data);

        };

I want to push the Filters if it is not available already. How can i do this.

I have tried above but dint work. What went wrong. Can anyone please help me,

Thanks,

1
  • Just a tip: prefixing your function parameters with $ is going to be confusing for other developers. When using AngularJS, only the core Angular functions are prefixed with $ so you know what is core AngularJS functionality, and what is your code. Commented Nov 27, 2014 at 5:59

2 Answers 2

2

So I am assuming that $scope.data.Filters is an array of objects with a FilterName and FilterValue property.

In that case, you actually need to search the array to see if a matching object exists before inserting it, by comparing the values of the properties of the object (a deep equality check, as opposed to a shallow equality check, which indexOf() does).

If you use lodash or underscore, you can use the _.findWhere() helper to do this easily:

if (!_.findWhere($scope.data.Filters, $scope.data1)) {
    $scope.data.Filters.push($scope.data1);
}

Otherwise, you could make your own function, so your full code looks like:

$scope.addField = function ($type, $value) {
    $scope.data1 = {
        FilterName: $type,
        FilterValue: $value
    };
    if (!filterExists($type)) {
        $scope.data.Filters.push($scope.data1);
    }
    $scope.data1 = "";
    $scope.Json = angular.toJson($scope.data);
};

function filterExists(type) {
    for (var i = 0, len = $scope.data.Filters.length; i < len; i++) {
        if ($scope.data.Filters[i].FilterName === type)
            return true;
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Backtrack Don't forget to accept the answer if it worked for you.
0

Try that:

$scope.addField = function ($type, $value) {
        $scope.data1 = {
            FilterName: $type,
            FilterValue: $value
        };
        if ($scope.data.Filters[$type] == undefined) {
            $scope.data.Filters[$type] = $scope.data1;
        }
        $scope.data1 = "";
        $scope.Json = angular.toJson($scope.data);

    };

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.