如何在JavaScript中使用根节点迭代json
我有一个带有根节点的json:如何在JavaScript中使用根节点迭代json,javascript,json,reactjs,Javascript,Json,Reactjs,我有一个带有根节点的json: { “评论”: [ {“id”:1,“author_name”:null,“comment_text”:null,“url”:http://localhost:3000/comments/1.json"} ] }您可以使用如下for循环对其进行迭代: data = { "comments": [ { "id": 1, "author_name": null, "
{
“评论”:
[
{“id”:1,“author_name”:null,“comment_text”:null,“url”:http://localhost:3000/comments/1.json"}
]
}
您可以使用如下for循环对其进行迭代:
data = {
"comments": [
{
"id": 1,
"author_name": null,
"comment_text": null,
"url": "http://localhost:3000/comments/1.json"
}, {
"id": 2,
"author_name": null,
"comment_text": null,
"url": "http://localhost:3000/comments/2.json"
}
]
}
for (var i = 0; i < data.comments.length; i++) {
var comment = data.comments[i];
// do something with the comment
// console.log(comment.id);
// console.log(comment.url);
}
您是如何定义变量this.props的?@TaoP.R.,忘了提到,这实际上是React。Map是用于数组而不是对象的。
var authors = data.comments.map(function(comment) {
return comment.author_name;
});