TypeScript中的forEach循环问题

时间:2018-08-17 15:10:11

标签: typescript sharepointframework

我的TypeScript代码有问题,我的forEach循环在页面上显示未定义。下面提供的是我的forEach语句的代码:

private _renderList(items: ISPList[]): void {
    let html: string = '<div class="ms-Grid"><div class="ms-Grid-row">';
    items.forEach((item: ISPList) => {
      html += `
      <div class="ms-Grid-col ms-u-sm4 ms-u-md4 ms-u-lg4">
      <h2>${item.DisplayListTitle}</h2>
      <div>${item.ListItems.length}</div>
      <div>${item.ListItems.forEach((listItem: ListObject,index) => {

      })}</div>
      </div>`;
    });
    html +="</div></div>";    
    const listContainer: Element = this.domElement.querySelector('#spListContainer');
    listContainer.innerHTML = html;
  }

正在通过_getMockListData方法填充数据(见下文):

private _getMockListData(): Promise<ISPLists> {
    return MockHttpClient.get()
      .then((data: ISPList[]) => {
        var listData: ISPLists = { value: data };
        return listData;
      }) as Promise<ISPLists>;
  }

MockHttpClient对象定义如下:

export default class MockHttpClient  {
   //Add some mock data to this for testing.
   private static _createListItems: ListObject[] = [
       {Title:'Doc1', FileType:'docx',ItemUrl:'http://www.espn.com', ItemType:'Document Library'},
       {Title:'Doc2', FileType:'docx',ItemUrl:'http://www.espn.com', ItemType:'Document Library'}];
   public static _applyListItems: ListObject[] = [
        {Title:'ListItem 1', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'},
        {Title:'ListItem 2', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'}];
   public static _deployListItems: ListObject[] = [
            {Title:'ListItem 2', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'},
            {Title:'ListItem 3', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'}]; 
   public static _supportListItems: ListObject[] = [
                {Title:'ListItem 5 ', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'},
                {Title:'ListItem 6', FileType:'ListItem',ItemUrl:'http://www.espn.com', ItemType:'List'}]; 

   private static _items: ISPList[] = [
       { Title: 'Mock List',   Id: '1', ListType:"List", DisplayListTitle:'CREATE',  RowCount: 4, SortOrder:1, ShowOnHomePage:true,ListItems:MockHttpClient._createListItems},
       { Title: 'Mock List 2', Id: '2', ListType:"Library", DisplayListTitle:'APPLY', RowCount: 4, SortOrder:2, ShowOnHomePage:true,ListItems:MockHttpClient._applyListItems },
       { Title: 'Mock List 3', Id: '3', ListType:"List", DisplayListTitle:'DEPLOY',RowCount: 4, SortOrder:3, ShowOnHomePage:true,ListItems:MockHttpClient._deployListItems },
       { Title: 'Mock List 4', Id: '4', ListType:"Library", DisplayListTitle:'SUPPORT', RowCount: 4, SortOrder:4, ShowOnHomePage:true,ListItems:MockHttpClient._supportListItems }];

   public static get(): Promise<ISPList[]> {
   return new Promise<ISPList[]>((resolve) => {
           resolve(MockHttpClient._items);
       });
   }
}

下面提供的是ISPList和ListObject的定义:

export interface ISPLists { value: ISPList[]; }
export interface ISPList {
  Title: string;
  Id: string;
  DisplayListTitle: string;
  RowCount: number;
  SortOrder: number;
  ShowOnHomePage: boolean;
  ListType: string;
  ListItems: Array<ListObject>;
}
export interface ListObject {
    Title: string;
    ItemUrl: string;
    ItemType: string;
    FileType: string;
}

我可以显示item.ListItems.length属性,它显示我有2个元素,加上我可以运行item.ListItems [0] .Title,它显示了实际的标题值。感觉就像我需要一个for循环而不是forEach循环。

请帮助。

2 个答案:

答案 0 :(得分:1)

该行:-

item.ListItems.forEach((listItem: ListObject,index) => {

      }

不执行任何操作并返回未定义。 因此,您需要制作一个返回字符串的函数。 forEach将始终返回未定义。

您可以使用地图。在这种情况下,这将返回一个字符串数组,可以将其连接在一起以组成一个字符串。

item.ListItems.map((listItem: ListObject,index) => `<a href="${
listItem.ItemUrl}">${listItem.Title}</a>`).join('\n')

答案 1 :(得分:0)

Array.forEach不返回值。与for循环相同,您必须定义一个外部变量并分配值。

改为使用map

const titles = item.ListItems.map((listItem: ListObject,index) => {
  return listItem.Title;

  // join array string to a string with "," delimiter
}).join(", ");