0

I'm trying to make my Angular controller more generic in the way it handles JSON array of objects.

Currently I have the following defined:

 $scope.Data = [];

and I pull across the data in my template like:

ng-repeat="item in Data.category[3].values

My JSON looks like:

 "category": [{
"name": "cat1",
"behaviour": "normal",
"selected": 0,
"values": [{
    "label": "define",
    "count": 6
}]
}, {
"name": "cat2",
"behaviour": "normal",
"selected": 0,
"values": [{
    "label": "type",
    "count": 6
}]
}, {
"name": "Company",
"behaviour": "multi-select",
"selected": 0,
"values": [{
    "label": "VW",
    "count": 4
}, {
    "label": "Renault",
    "count": 1
}, {
    "label": "Fiat",
    "count": 1
}]
}, {
"name": "Make",
"behaviour": "multi-select",
"selected": 0,
"values": [{
    "label": "Gold",
    "count": 3
}]
}, {
"name": "Color",
"behaviour": "normal",
"selected": 0,
"values": [{
    "label": "White",
    "count": 3
}, {
    "label": "Blue",
    "count": 2
}, {
    "label": "Green",
    "count": 1
}]
}]

What I'm trying to do is rather than access a specific index with:

 Data.category[3].values

I'd like to loop through category and grab a list of index items so I can use like:

Data.category[Color].values 

where Color is found in the list, and I can bring back the values for that specific element, is that possible?

7
  • color is an attribute in values or in category? Commented Jun 3, 2015 at 9:39
  • So if I understand correctly you want to do the same as you are doing with category now, e.g. making it something like: Data.category.color.values? Commented Jun 3, 2015 at 9:41
  • learn how to use Array.prototype.filter() Commented Jun 3, 2015 at 9:44
  • @charlietfl possible using anglar pipe filter Commented Jun 3, 2015 at 9:47
  • 1
    @pankajparkar depends on what is being done. Use case isn't very clear Commented Jun 3, 2015 at 9:48

2 Answers 2

1

You would use filter instead, you can also pass Color value dynamically through scope.

Markup

ng-repeat="item in (Data.category| {name: 'Color'}: true)[0].values

Update

You could pass Color variable dynamically through another scope variable, like $scope.color = 'Color' so the ng-repeat will become

Html

ng-repeat="item in (Data.category| {name: color}: true)[0].values
Sign up to request clarification or add additional context in comments.

1 Comment

I think this would be the best for my usecase, as I want to specify name:value, however at the moment once I change, I don't seem to get anything back?
0

I am not sure this is what you want but if you need to display value in function of color attribute you could do this in your controller:

      $scope.dataToDisplay = [];
      for(var i = 0; i < $scope.Data.length ; i++){
     {
        if($scope.Data.color == 'yourcolor') {
              $scope.dataToDisplay.push($scope.Data[i].values)
           }
      }

And in your HTML you just display dataToDisplay

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.