[英]How can i loop through json data?
I am trying to learn node.js but at some point i got stuck. 我正在尝试学习node.js,但有时我陷入了困境。 This is my json input : [ { "FIELD1": "name", "FIELD2": "id"}, { "FIELD1": "abc", "FIELD2": "12"}]
这是我的json输入:[{“ FIELD1”:“名称”,“ FIELD2”:“ id”},{“ FIELD1”:“ abc”,“ FIELD2”:“ 12”}]
How can i loop through this,i tried 'for' but it didn't work. 我该如何循环,我尝试了“ for”,但没有成功。 can any one help ?
有谁可以帮忙吗?
You're trying to loop over an array of json objects, so you could just 您正在尝试遍历json对象数组,因此您可以
for(let i= 0; i < object.length; i++){
//access your object fields
console.log(object[i].FIELD1);
console.log(object[i].FIELD2);
}
well, first of all you are looping over an array. 好吧,首先,您要遍历数组。 That you can do.
那你可以做。
For looping over a json, you can either get the keys. 对于循环遍历json,您可以获取密钥。 I believe
.keys()
. 我相信
.keys()
。
I also believe you can do for(var x in json){}
我也相信您可以做到
for(var x in json){}
Basically you can use 3 opportunities here: 基本上,您可以在这里使用3个机会:
for(let t = 0; t < data.length; t++) { ... }
) for(let t = 0; t < data.length; t++) { ... }
) for(const el of data) { ... }
) for(const el of data) { ... }
) Assuming you have such data: 假设您有以下数据:
const data = [ { "FIELD1": "name", "FIELD2": "id"}, { "FIELD1": "abc", "FIELD2": "12"}];
You can loop through it: 您可以遍历它:
data.forEach(el => console.log(el));
The one you've tried: for(let prop in obj)
is used to iterate through objects, not arrays. 您尝试过的一个:
for(let prop in obj)
中使用for(let prop in obj)
用于遍历对象,而不是数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.