为什么我不能在对象数组中的for-in循环中访问对象属性? [英] Why can’t I access object properties in a for-in loop over an array of objects?
问题描述
我目前正在使用SheetSU API从Google表格文件中读取数据。
I am currently utilizing the SheetSU API to read data from a Google Sheets file.
var members = Sheetsu.read("URL", {}, successFunc);
现在, successFunc
是一个函数将输入 data
输入 console.log()
给我。
For now, the successFunc
is a function that takes the input data
to console.log()
to me.
数据导入如下:(14){{..},{..},{..} ....}
和每个对象在这本词典中看起来像这样:
The data imports like this: (14){{..},{..},{..}....}
and each object within this dictionary looks something like this:
0: {Name: "First Last", ID: "12536723", Status: "Member"}
1: {Name: "First Last", ID: "12371238", Status: "Member"}
...
14: {Name: "First Last", ID: "12341228", Status: "Member"}
我需要提取值姓名
和 ID
,但是:
I need to pull out the values of Name
and ID
, however:
for (var x in data) {
console.log(x)
}
= 0,1,2,...,14
(因为不是对象)
= 0, 1, 2, ..., 14
(as ints not objects)
for (var x in data) {
console.log(x.Name)
}
= undefined,undefined,...,undefined
推荐答案
x
不是值它是索引
比如0,1 ... 14,您可以使用的
,或者在<$内使用 data [x]
C $ C>在。
x
is not a value It is a index
like 0,1 ...14, you can use of
for that or use data[x]
inside in
.
var data = [{
Name: "First Last",
ID: "12536723",
Status: "Member"
}, {
Name: "First Last",
ID: "12371238",
Status: "Member"
},
{
Name: "First Last",
ID: "12341228",
Status: "Member"
}
]
for (var x in data) {
console.log(data[x].Name); //x is not a value It is a index like 0,1 ...14
}
for (var x of data) {
console.log(x.Name); // use of which prints x as object not index
}
这篇关于为什么我不能在对象数组中的for-in循环中访问对象属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!