【发布时间】:2019-10-18 16:28:48
【问题描述】:
我正在设置我的代码并遇到问题.. 这段代码只是我想要做的一个例子。 所以基本上 config.Te[i].ref 应该读取 Te.. 中的所有 ref,但它只读取 Te.. 中的第一行而不是所有行。
问候
for (var i in config.acc,config.Te,config.Hu,config.Ve)
console.log(config.Te[i].ref)
{}
当我将日志输出更改为 config.acc[i].ref 或其他时,它也只读取该特定配置的第一行。
但是当我将其更改为 for (var i in config.acc) 时,它会读取所有参考...
所以我认为我的代码格式错误,我不知道如何修复它。 错误必须在分离配置中。 我只想一一称呼他们。
"acc":
[
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 },
{"type":"PIR", "ref":0 }
],
【问题讨论】:
-
for (var i in config.acc,config.Te,config.Hu,config.Ve)等价于for (var key in config.Ve)MDN Comma Operator 我假设config.Ve只有 1 行。 -
@Thomas 不,他们都有很多行..这就是为什么我问如何解决这个问题...就像我想通过 console.log(config.Te[i].ref 调用参考) 等等
-
“我想通过console.log(config.Te[i].ref)等方式调用refs”
for (var i in config.Te) console.log(config.Te[i].ref);怎么样 -
@Thomas 谢谢,我已经知道了。就像上面的问题一样,我怎样才能做到这一点,因为上面的代码只给了我 1 行而不是每个数组的所有内容......事实上,如果我使用 for (var i in config.Te) 这将给出我只需要这个信息..其余的呢?我只想将它们合并到一个代码中。这样我就可以调用它了,console.log(config.acc[i].ref) console.log(config.hu[i].ref) console.log(config.ve[i].ref)。
-
最后,我想我明白你的意思了
for(var i=0, length=Math.max(config.acc.length,config.Te.length,config.Hu.length,config.Ve.length); i<length; ++i) { ... }
标签: javascript node.js loops for-loop var