2***@qq.com
2***@qq.com
  • 发布:2025-01-16 10:35
  • 更新:2025-01-16 10:35
  • 阅读:52

uts中,reduce一直报错,怎么回事?

分类:uni-app x

后端返回的接口,成功code == 200后,携带data,其中data里有一个名为dtoList的数组,我需要叠加dtoList的money字段,求出总金额,使用数组reduce方法,一直编译不过去:

[⁠java.lang.ClassCastException⁠]‍ {cause: null, message: "io.dcloud.uts.UTSArray cannot be cast to io.dcloud.uts.UTSJSONObject"}


try {  
            const res = await getMedMoney();  

            if (res['code'] == 200) {  
                let resData = (res['data'] as UTSJSONObject);  

                let _listDto = resData['listDto'] as Array<UTSJSONObject>;  

 //这里一直报错:[⁠java.lang.ClassCastException⁠]‍ {cause: null, message: "io.dcloud.uts.UTSArray cannot be cast to io.dcloud.uts.UTSJSONObject"}  

                let _allMoney = _listDto.reduce((prev : number, curr : UTSJSONObject) => {  
                    return (curr['money'] as number) + prev;  
                }, 0)  

                              console.log("_allMoney");  

            }  
        } catch (err : any) {  
            console.log("这里报错了!!!!!!")  
            console.error(err);  
        }
2025-01-16 10:35 负责人:无 分享
已邀请:

要回复问题请先登录注册