【问题标题】:How to get total of numbers in an array from SharePoint list items如何从 SharePoint 列表项中获取数组中的总数
【发布时间】: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

标签: reactjs spfx


【解决方案1】:

创建一个函数来循环遍历项目并添加小时

function countHours(items) {
  if (!items) {
    return 0;
  }

  let total = 0;

  for (let i = 0; i < items.length; i++) {
    total += items[i].Hours;
  }

  return total;
}

const item = [
  { Id: 25, Title: "Comp Time", Hours: 6, ID: 25 },
  { Id: 26, Title: "Comp Time", Hours: 5, ID: 26 },
  { Id: 27, Title: "Work from Home", Hours: 3, ID: 27 },
  { Id: 28, Title: "Comp Time", Hours: 7, ID: 28 },
  { Id: 29, Title: "Work from Home", Hours: 8, ID: 29 },
  { Id: 30, Title: "Holiday", Hours: 8, ID: 30 },
  { Id: 31, Title: "Work from Home", Hours: 32, ID: 31 }
];

console.log(countHours(item));

像这样使用它

this.setState({
    items: item,
    hoursTotal: countHours(item)
});

你也可以使用reduce

const item = [
  { Id: 25, Title: "Comp Time", Hours: 6, ID: 25 },
  { Id: 26, Title: "Comp Time", Hours: 5, ID: 26 },
  { Id: 27, Title: "Work from Home", Hours: 3, ID: 27 },
  { Id: 28, Title: "Comp Time", Hours: 7, ID: 28 },
  { Id: 29, Title: "Work from Home", Hours: 8, ID: 29 },
  { Id: 30, Title: "Holiday", Hours: 8, ID: 30 },
  { Id: 31, Title: "Work from Home", Hours: 32, ID: 31 }
];
const sum = item.reduce(function(a, b) { return a + b.Hours; }, 0);

console.log(sum)

【讨论】:

    【解决方案2】:

    在不知道数据结构是什么样子的情况下很难回答这个问题,但如果您尝试对一组数字求和,则可以使用 reduce

    const hours = [7, 5, 3, 1, 7]
    
    const totalHours = hours.reduce((accumulator, hour) => accumulator + hour)
    

    【讨论】:

    • 嗨 Nitsew...我想我在某个时候尝试过,但数组中不仅仅是直数,我无法弄清楚该语法...对不起,我没有包括那要开始了。如果有帮助,下面是一个返回的示例: 1:小时:6 ID:25 Id:25 标题:“Comp Time”proto:对象 2:小时:5 ID:26 ID:26 标题: "Comp Time" proto: Object
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多