【发布时间】:2019-05-03 00:15:13
【问题描述】:
我正在使用 Reactjs 创建一个 spfx Web 部件。我有一个函数从 SharePoint 列表中获取一组项目,其中包括“小时”的数字列。我需要计算所有返回的小时数,但不知道如何计算。
我觉得我错过了一些简单的东西,但我已经经历了各种循环,由于某种原因我无法让它工作。我已确认我正在从“小时”列获取数据。
我还将声明“我是 spfx 的新手并做出反应”。 :) TIA 寻求帮助!
private readItem(): void {
this.props.spHttpClient.get(`${this.props.siteUrl}/_api/web/lists/getbytitle('Time Off')/items?$select=Title,Id,Hours`,
SPHttpClient.configurations.v1,
{
headers: {
'Accept': 'application/json;odata=nometadata',
'odata-version': ''
}
}).then((response: SPHttpClientResponse): Promise<ITimeOffItem[]> => {
return response.json();
})
.then((item: ITimeOffItem[]): void => {
console.log(item); //the data is here including Hours
this.setState({
items: item,
hoursTotal: //????How do I get the sum of "Hours" and assign it to a number in state
});
});
}
【问题讨论】:
-
console.log(item);的输出是什么? -
这里是输出 {value: Array(8)} value: Array(8) 0: Hours: 4 ID: 24 Id: 24 Title: "Work from Home" proto:对象 1:{Id:25,标题:“Comp Time”,小时:6,ID:25} 2:{Id:26,标题:“Comp Time”,小时:5,ID:26} 3:{ ID:27,标题:“在家工作”,小时:3,ID:27} 4:{ID:28,标题:“Comp Time”,小时:7,ID:28} 5:{ID:29,标题:“在家工作”,时间:8,ID:29} 6:{ID:30,标题:“假期”,时间:8,ID:30} 7:{ID:31,标题:“在家工作” , Hours: 32, ID: 31} length: 8 proto: Array(0) proto: Object
-
抱歉 - 复制粘贴看起来并没有扩展所有内容...我扩展了其他一些内容,希望这更有帮助:{value: Array(8)} value: Array(8) 0:小时:4 ID:24 ID:24 标题:“在家工作”proto:对象 1:小时:6 ID:25 ID:25 标题:“Comp Time”proto:对象 2:小时:5 ID:26 ID:26 标题:“Comp Time”proto:对象/////跳过一些空间//// 7:小时:32 ID:31 ID:31 标题:“在家工作”proto:对象长度:8 proto:Array(0) proto:Object