[英]iterate through objects and array javascript/angular
I am trying to iterate an object that I get in Angular but I was unable to. 我正在尝试迭代我在Angular中获得的对象,但我无法这样做。 In order to understand that I tried it simply using below code:
为了理解我只是使用以下代码尝试了它:
<script>
var x = {"data":['A','B','C']};
for(v in x)
{
alert(v[0]);
}
</script>
The output of this is "d".How can I output "A"? 此输出为“ d”。如何输出“ A”?
If you use this for loop, this is the syntax: 如果使用此for循环,则语法如下:
var x = {"data":['A','B','C']};
for(var key in x)
{
alert(key); //data
alert(x[key]); //A,B,C
}
This is plain JS though, no Angular. 不过这是纯JS,没有Angular。
You can add further if
clauses to receive the first element like A
, but make sure it doesn't error out on other properties of the object. 您可以添加更多的
if
子句来接收第一个元素,如A
,但要确保它不会在对象的其他属性上出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.