1

I am trying to filter my JSON object by a specific property value set to Log: true

If an object has this property set to false, I want to filter it out. Here is an example of the JSON structure:

$scope.Main =
    { 
          "MyBook" :  
          {
            "Title": "The Road",
            "Type" : "Text",
            "Log" : false
          },
          "MyCat":
          {
            "Name" :  "Penny",
            "Type" :  "Pet",
            "Log" : true            
          },
          "Car":
          {
            "Make" :  "Toyota",
            "Model" : "Camry",
            "Type" :  "Vehicle",
            "Log" : false     
          }
    }

As you can see, the objects themselves are not similar, but they all contains a log property.

2
  • Could you elaborate a bit more? Is this within an ng-repeat or something? The solution is to use Filters, but we need more details. docs.angularjs.org/api/ng/filter/filter Commented May 30, 2014 at 0:09
  • This would happen in a function in a controller Brandon. Commented May 30, 2014 at 0:25

3 Answers 3

1

Online Demo

This is how I would filtered an object while searching for a property value equals true

var sampleObj = {/* you sample object*/}; 

var filtered = Object.keys(sampleObj).reduce(function(arr,prop){
    if(Object.keys(sampleObj[prop])
        .filter(function (p) {return p === "Log";})){
            if(sampleObj[prop].Log==true){
                arr.push(sampleObj[prop]);
            }

    }
    return arr;
},[]);
console.log(filtered);

Since you are using angular probably you would want to use a custom filter instead:

Something close to:

custom filter:

angular.module('myApp', []).filter('myFilter', function() {
    return function(sampleObj, param1) {
        return Object.keys(sampleObj).reduce(function(arr,prop){
            if(Object.keys(sampleObj[prop])
                .filter(function (p) {return p === "Log";})){
                if(sampleObj[prop].Log==param1){
                    arr.push(sampleObj[prop]);
                }
            }
            return arr;
        },[]);
    };
});

and in your html

 <li ng-repeat="item in sampleObj | myFilter: true">
Sign up to request clarification or add additional context in comments.

Comments

0

try using the underscorejs library.

you can use some of their functions like _.filter and _.has to filter the list.

here's an example of how i would try to implement that object:

var filtered = _.filter($scope.Main, function(obj) {
    return _.has(obj, "Log") && obj.Log;
}

Comments

0

Use a custom Angular filter:

.filter('filterLog', function(){
  return function(items){
    for (var item in items) {
      if (items[item].Log === false) delete items[item];
    }
    return items;
  }
})

Then, in your view, you could output the filtered list like so:

<li ng-repeat="(key, value) in Main | filterLog">{{value}}</li>

If you need to use it in a controller, you could:

$scope.filtered = $filter('filterLog')($scope.Main);

Demo

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.