Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在react.js中计算数组中的数组数据?_Reactjs_Axios - Fatal编程技术网

Reactjs 如何在react.js中计算数组中的数组数据?

Reactjs 如何在react.js中计算数组中的数组数据?,reactjs,axios,Reactjs,Axios,我试图计算这些评论中的评论总数,但它只计算2个数据,我使用的是res.data.length,我不知道如何只计算5个数据,这是评论中唯一的评论,我的意思是应该计算5个数据,但它只计算2个数据 这里是json { "comments": [ { "comment": "comment 1.1" }, { "comment": "comment 1.

我试图计算这些评论中的评论总数,但它只计算2个数据,我使用的是
res.data.length
,我不知道如何只计算5个数据,这是评论中唯一的评论,我的意思是应该计算5个数据,但它只计算2个数据

这里是json

{
        "comments": [
            {
                "comment": "comment 1.1"
            },
            {
                "comment": "comment 1.2"
            }
        ],
        "date": "2019-10-22T20:21:04.927Z",
        "_id": "5daf65c8bcaab30224def48f",
        "caption": "caption 1",
        "picture": "https://ichef.bbci.co.uk/news/976/media/images/83351000/jpg/_83351965_explorer273lincolnshirewoldssouthpicturebynicholassilkstone.jpg",
        "__v": 0
    },
    {
        "comments": [
            {
                "comment": "comment 2.1"
            },
            {
                "comment": "comment 2.1"
            }
            {
                "comment": "comment 2.1"
            },
        ],
        "date": "2019-10-23T05:30:16.210Z",
        "_id": "5dafe7876688d3064073eed0",
        "caption": "ini caption 2",
        "picture": "https://helpx.adobe.com/content/dam/help/en/stock/how-to/visual-reverse-image-search/jcr_content/main-pars/image/visual-reverse-image-search-v2_intro.jpg",
        "__v": 0
    }
这是密码

componentDidMount() {
        const url = "http://localhost:5000/api/comments/";
        axios.get(url).then(res => {
          this.setState({
              isLoaded: true,
              comments: res.data,
              countComment: res.data.length
          })
        })      

    }

用解决方案扩展我的评论。转到第二个示例,查看刚刚编辑的代码的解决方案

//模拟axios响应
常数res={}
资源数据=[
{
评论:[
{
评论:“评论1.1”
},
{
评论:“评论1.2”
}
],
标题:“标题1”
},
{
评论:[
{
评论:“评论2.1”
},
{
评论:“评论2.2”
},
{
评论:“评论2.3”
}
],
标题:“标题2”
}
]
让commentCount=0
//迭代每个项目,您需要
res.data.forEach(数据=>{
commentCount+=data.comments.length
})
console.log(commentCount)//5
在您的场景中的用法:

componentDidMount(){
常量url=”http://localhost:5000/api/comments/";
get(url)。然后(res=>{
让commentCount=0
res.data.forEach(数据=>{
commentCount+=data.comments.length
})
这是我的国家({
isLoaded:是的,
评论:res.data,
countComment:commentCount
})
})      
}
您可以查看这些MDN文章:

这没关系,因为数组中有两个对象。res.data[0]=第一个“注释”包含2条注释,res.data[1]=第二个“注释”包含3条注释。这是一个简单的JS问题,与reactjs甚至axios没有太大关系。要获得评论总数(5),您需要遍历res.data[i]并计算“评论”长度。欢迎使用Stackoverflow。首先,尽量不要发布代码的图片,而是粘贴代码的相关部分,并使用“代码格式”按钮正确设置其格式。第二,确保包含相关代码。如果可能,请尝试提交实际代码,而不是代码截图。@Alf17请共享您的json代码。对不起,我放错了图片,这是我的代码