TypeScript 中 forEach 循环的问题
Problems with forEach loop in TypeScript
我的 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 object 定义如下:
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 循环。
请帮忙。
行:-
item.ListItems.forEach((listItem: ListObject,index) => {
}
什么都不做,return未定义。
所以你需要做一个 returns 字符串的函数。
forEach 总是 return undefined.
您可以使用地图。这 return 是一个数组,在本例中是字符串,可以将它们连接在一起形成一个字符串。
item.ListItems.map((listItem: ListObject,index) => `<a href="${
listItem.ItemUrl}">${listItem.Title}</a>`).join('\n')
Array.forEach
没有 return 值。和for循环一样,你必须定义一个外部变量并赋值。
改用map
:
const titles = item.ListItems.map((listItem: ListObject,index) => {
return listItem.Title;
// join array string to a string with "," delimiter
}).join(", ");
我的 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 object 定义如下:
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 循环。
请帮忙。
行:-
item.ListItems.forEach((listItem: ListObject,index) => {
}
什么都不做,return未定义。 所以你需要做一个 returns 字符串的函数。 forEach 总是 return undefined.
您可以使用地图。这 return 是一个数组,在本例中是字符串,可以将它们连接在一起形成一个字符串。
item.ListItems.map((listItem: ListObject,index) => `<a href="${
listItem.ItemUrl}">${listItem.Title}</a>`).join('\n')
Array.forEach
没有 return 值。和for循环一样,你必须定义一个外部变量并赋值。
改用map
:
const titles = item.ListItems.map((listItem: ListObject,index) => {
return listItem.Title;
// join array string to a string with "," delimiter
}).join(", ");