Terrtiary
Terrtiary

Reputation: 191

How to access all array values from JSON in angular

If I have a database schema set up like this

{ "name":  "Microsoft",
     "field": [
         "technology",
         "ai",
         "etc"
     ]
}

how can I get the data from angular? Calling:

{{company.field[]}}

doesn't work. I can only call an array index like

{{company.field[1]}}

Upvotes: 0

Views: 48

Answers (4)

Samuel Toh
Samuel Toh

Reputation: 19268

The property field is not nested under the name property.

To access the array values you'll need to do company.field[n] where n is the index of the item or just dump the entire array like company.field.

See my JSFiddle.

Upvotes: 1

Nainish Modi
Nainish Modi

Reputation: 488

 //try this
 <div ng-repeat="val in company">
    {{val.name}}
    <div ng-repeat="obj in val.field">
       {{obj.technology}}
       {{obj.ai}}
       {{obj.etc}}
    </div>
 </div>

Upvotes: 1

raj peer
raj peer

Reputation: 718

<script>

  var company =  { "name":  "Microsoft",
     "field": [
         "technology",
         "ai",
         "etc"
     ]
}
for(i=0;i<company.field.length;i++){
console.log(company.field[i])

}

</script>

Upvotes: 0

alexhuang
alexhuang

Reputation: 536

Since the field property is an array, are you looking to iterate through to get those values?

You can use the ng-repeat directive

<div ng-repeat="item in company.field">
{{item}}
</div>

Upvotes: 2

Related Questions