如何以角度访问JSON中的所有数组值

时间:2021-10-16 15:43:27

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]}}

4 个解决方案

#1


1  

The property field is not nested under the name property.

属性字段不嵌套在name属性下。

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.

要访问数组值,您需要执行company.field [n],其中n是项的索引,或者只是像company.field一样转储整个数组。

See my JSFiddle.

看我的JSFiddle。

#2


2  

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

由于field属性是一个数组,您是否希望迭代以获取这些值?

You can use the ng-repeat directive

您可以使用ng-repeat指令

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

#3


1  

 //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>

#4


0  

<script>

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

}

</script>

#1


1  

The property field is not nested under the name property.

属性字段不嵌套在name属性下。

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.

要访问数组值,您需要执行company.field [n],其中n是项的索引,或者只是像company.field一样转储整个数组。

See my JSFiddle.

看我的JSFiddle。

#2


2  

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

由于field属性是一个数组,您是否希望迭代以获取这些值?

You can use the ng-repeat directive

您可以使用ng-repeat指令

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

#3


1  

 //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>

#4


0  

<script>

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

}

</script>