1

I have json file called brands.json:

{
  "brands": {
    "Honda": [
      "Accord",
      "Civic"
    ],
    "Porsche": [
      "Cayenne",
      "Cayman"
    ]
  }
}

Im trying to iterate through this list and list the brand(e.g. Honda and Porsche) and render using HTML lists.

<li ng-repeat="brands in items">{{brands}}</li>

JS:

$scope.items= [];
$http.get('brands.json').then(function(response) {
    $scope.items =response.data.brands;
});

This code works fine but it displays the arrays inside the brand names e.g. instead if displaying Honda it displays ["Accord", "Civic"]. I want it to display the brand names only.

<li>Honda</li>
<li>Porsche</li>

1 Answer 1

5

Try:

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

Quouted from the docs:

(key, value) in expression – where key and value can be any user defined identifiers, and expression is the scope expression giving the collection to enumerate.

For example: (name, age) in {'adam':10, 'amalie':12}.

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

3 Comments

do you know how i can get the arrays of the "key" once its clicked? using ng-click for exaple if i click honda console.log Accord, Civic
@user2681696: you just need to access the value. For example: <li ng-repeat="(key, value) in items" ng-click="show(value)">{{key}}</li>
Thanks :) much appreiated

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.