2

Listed all the array of object as a lists using ng-repeat and added checkbox for each value.

Here I want to filter the checkbox and form a new JSON on click Apply Filter based on check/uncheck the value(Checkbox's).

Actual

I've tried adding the selected and unselected checkbox in the models scope object($scope.models) by concatenating the filter name and its values

(i.e) : data1(filter name)+1(value) = data11

On clicking apply filter looping the existing filters array and compare with the model object and push the matches in the new array.

Plunker

Apply Filter function

HTML

<li ng-repeat="(key, filter) in filters">
<a href="" data-target="#{{filter.name}}" data-toggle="collapse" aria-expanded="{{enableShow.indexOf(filter.name) > -1 ? true : false}}">
    {{filter.name}}
</a>
<ul class="collapse list-unstyled" id="{{filter.name}}" ng-class="{show : enableShow.indexOf(filter.name) > -1}">
    <li ng-repeat="v in filter.values">
        <label class="w-100" ng-show="filter.type == 'CheckBox'">
            <span class="ml-3 p-1 d-block">
                {{v.value}}
                <input type="checkbox" ng-model="models[filter.name + v.value]" class="pull-right mt-1 ml-1" style="margin-right: 7%" />
            </span>
        </label>
    </li>
</ul>

JS

$scope.applyFilters = function() {
var arr = [];
_.map($scope.filters, function(d) {
    _.map(d.values, function(v) {
        var name = d.name + v.value;
        for (key in $scope.models) {
            if (key == name) {
                arr.push({
                    name: d.name,
                    values: v
                });
            }
        }
    });
});
console.log(arr);
};

Expected

On clicking apply filter want to form a new JSON which contains only the selected values in its respective object.

{
"results": [
    {
        "name": "data1",
        "type": "CheckBox",
        "values": [
            {
                "value": "1"
            },
            {
                "value": "4"
            }
        ]
    },
    {
        "name": "data2",
        "type": "CheckBox",
        "values": [
            {
                "value": "1"
            }
        ]
    },
    {
        "name": "data5",
        "type": "CheckBox",
        "values": [
            {
                "value": "3"
            }
        ]
    },
    {
        "name": "data6",
        "type": "CheckBox",
        "values": [
            {
                "value": "2"
            }
        ]
    }
]
}

Thanks in advance.

2
  • Please include the HTML on which this code operates. Commented Jan 5, 2019 at 19:20
  • When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. That code should be… Minimal – Use as little code as possible that still produces the same problem. See How to create a Minimal, Complete, and Verifiable example. Commented Jan 5, 2019 at 19:32

2 Answers 2

0

Iterate the original data structure with _.map() and transform it to the requested format. Use _.filter() or _.reject() to remove sections without selected values (plunker):

$scope.applyFilters = function() {
  var arr = _($scope.filters)
    .map(function(o, i) {
      var section = _.pick(o, ['name', 'type']);

      section.values = _(o.values)
        .filter(function(v, j) {
          return $scope.models['data' + (i + 1) + (j + 1)];
        })
        .map(_.partialRight(_.pick, 'value'))
        .value();

      return section;
    })
    .reject(function(o) { return _.isEmpty(o.values); })
    .value();

  console.log(arr);
};
Sign up to request clarification or add additional context in comments.

Comments

0

You could use obj.hasOwnProperty(prop) to contains only the selected values in its respective object.

Just change your applyFilters to this:

 $scope.applyFilters = function() {
    var arr = [];
    _.map($scope.filters, function(d) {
      _.map(d.values, function(v) {
        var name = d.name + v.value;
        for (key in $scope.models) {
           if (key == name && $scope.models.hasOwnProperty(key) && $scope.models[key]) {
            arr.push({
              name: d.name,
              values: v
            });
          }
        }
      });
    });
    console.log(arr);
  };

plunker: https://next.plnkr.co/edit/waIwyXgEkdfPtybq

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.