1

My scope data:

$scope.data = 
 "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
}]
}]

How can I access the "name":"value" without using indexes? as the data might grow and change and I don't want to assign an index value anywhere? I'd still want to filter such as:

| {name: 'Make'}: true)

in my mark up to show

1

2 Answers 2

1

So the json is incorrect, i corrected it (next time you can use this site to see if your json is valid JSONVALIDATOR) If you want to access at value of the name do this

In your controller :

$scope.data = [
{
    "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
        }
    ]
}
];

In your HTML

      <div ng-repeat="d in data"> {{d.name}} </div>

If you want to display values of the object with the name 'Company' you can do this like in my working CodePen :

Just add a new ng-repeat :

   <div ng-repeat="d in data"> {{d.name}} 
         <div ng-if="d.name == 'Company'" ng-repeat="da in d.values">{{da.label}}</div>      
  </div>
Sign up to request clarification or add additional context in comments.

4 Comments

I need to be able to specify however that name=Company for example
If this is Company you do something different? Is it that you mean?
So no, I would do for example: <div ng-repeat="d in data | {name: 'Company'}: true)"> and show a count so value for example
Codepen added to my answer
0

First off, the format of your data is not correct JSON format, run it through an online validator.

To access the element, you would simply be able to through:

<li ng-repeat="item in data">
    {{ item.name }} {{ item.behaviour }} etc...
</li>

Get rid of category within $scope.data... just let it be an array like a typical JSON value.

2 Comments

@carton - ha, must have posted at the same time.. Simple enough issue
I can't play about with the structure :)

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.