0

I want to map a $scope.filters object to a var criteria on the condition if the original fields are null or not.

So lets say I have:

$scope.filters = {
    name: 'myName'
    lastName: null,
    age: null,
}

I want my var criteria to be mapped to non null fields like this:

var criteria = {
    name: 'myName';
}

So I have tried like this:

var criteria = {};

angular.forEach($scope.filters, function (value, key, obj) {
        if (value != null) {
            this.push(obj)
        }
}, criteria);

But I guess I'm missing something.

1
  • What are you pushing onto? Your $scope.filters is an Object, not an Array. Commented Oct 2, 2014 at 10:59

2 Answers 2

1

criteria is an object, not an array, so you can't push it. Either make it an array if you want to store separate individual criteria, or replace the push with this[key] = value;

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

Comments

1

You should do it like this,

$scope.filters = {
  name: 'myName'
  lastName: null,
  age: null,
 }

var criteria = []; //array, you were using object
    angular.forEach($scope.filters, function(value, key) {
      if (value != null) {
            this.push(key + ': ' + value);
        }

    }, criteria);  

expect(criteria).toEqual(['name: myName']);

Hope it helps.

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.