将exactPageList分配给pagefield时如何将其从any转换为any[]?

Posted

技术标签:

【中文标题】将exactPageList分配给pagefield时如何将其从any转换为any[]?【英文标题】:How to convert exactPageList from any to any[] when assign it to pagefield? 【发布时间】:2020-09-17 08:44:32 【问题描述】:

我在 Angular 7 应用程序上工作,我在分页时遇到问题无法将 exactpagelist any 分配给 pagefield any[]。

因为 pagefield 是 array 类型,exactpagelist 是 any 类型。

此行的函数 totalNoOfPages 的最后一行存在问题

this.pageField = this.exactPageList;
this.pageField = 2; not correct

我希望是

this.pageField = [1,2];

意思是我需要转换 this.exactPageList;要接受的数组分配给页面字段 该怎么做?

pageField:any[];
exactPageList:any;
totalNoOfPages()   

    this.paginationData = Number(this.totalReportCount / this.ReportPerPage);  
    console.log("pagination data :" + this.paginationData)
    let tempPageData = this.paginationData.toFixed(); 
    console.log("tempPageData data :" + tempPageData) 
    if (Number(tempPageData) < this.paginationData)   
      this.exactPageList = Number(tempPageData) + 1;  
      this.paginationService.exactPageList = this.exactPageList;  
      console.log("exactPageList1  data :" + this.exactPageList ) 
     else   
      this.exactPageList = Number(tempPageData);  
      this.paginationService.exactPageList = this.exactPageList  
      console.log("exactPageList2  data" + this.exactPageList ) 
      
    this.paginationService.pageOnLoad();  
    this.pageField = this.exactPageList;    

    

上面的代码结果如下:

pagination data1.0666666666666667
reportdetails.component.ts:265 tempPageData data1
reportdetails.component.ts:269 exactPageList1  data2
reportdetails.component.ts:263 pagination data1.0666666666666667
reportdetails.component.ts:265 tempPageData data1
reportdetails.component.ts:269 exactPageList1  data2

预期结果

this.pageField = [1,2];

【问题讨论】:

我不确定你真正想要什么,但你可以通过 this.pageField = [ this.exactPageList ] 创建一个包含单个数字条目的数组 - 尽管我假设 this.exactPageList 是一个数组并且由于它的名字没有编号。我要么已经将exactPageList 定义为数组,要么重命名它。 请解释 this.exactPageList 包含的内容。我回应@Paul。如果它包含值数组,Paul 的答案将起作用。 它确实包含一个数字。检查this.exactPageList = Number(tempPageData) + 1; this.exactPageList = Number(tempPageData)这行我不明白的是,为什么它应该包含[1, 2],这些数字是从哪里来的? exactpagelist 类型为 any 包含数字为 2 我需要 [1,2] 分配给 pagefield 那么,如果exactpagelist4 那么应该pageField = [1, 2, 3, 4] 吗? 【参考方案1】:

使用 cmets 的一些解释:

this.paginationData = Number(this.totalReportCount / this.ReportPerPage);  
console.log("pagination data :" + this.paginationData)
// Use Math.round instead. Then you get an numeral and not a string.
let tempPageData = this.paginationData.toFixed(); 
console.log("tempPageData data :" + tempPageData) 
// Then Number() is not necessary here anymore.
// Moreover as far as I understand, you actually want to use always the 'bigger number'. 
// That means, actually it would make sense to use Math.ceil() above.
// In that case, you don't need to use a condition at all.
if (Number(tempPageData) < this.paginationData)   
  this.exactPageList = Number(tempPageData) + 1;  
  this.paginationService.exactPageList = this.exactPageList;  
  console.log("exactPageList1  data :" + this.exactPageList ) 
 else   
  // You are doing almost the same like in the condition clause above.
  // It would be enough to set this.exactPageList condtionally and do the rest afterwards.
  this.exactPageList = Number(tempPageData);  
  this.paginationService.exactPageList = this.exactPageList  
  console.log("exactPageList2  data" + this.exactPageList ) 
  
this.paginationService.pageOnLoad();  
this.pageField = this.exactPageList; 

我会做什么:

this.paginationData = Number(this.totalReportCount / this.ReportPerPage);  
console.log("pagination data :", this.paginationData)
let tempPageData = Math.ceil(this.paginationData);
console.log("tempPageData data :", tempPageData) 
// Let's create an array from 1 .. N
// [...Array(4).keys()] would create an array with [0,1,2,3]
// but we want to have [1,2,3,4], that's why we are incrementing every value by using map(). 
// Think about moving this to an own method 'getOneToNArray' or so for a better readability.
this.exactPageList = [...Array(tempPageData).keys()].map(x => ++x);;
this.paginationService.exactPageList = this.exactPageList;  
console.log("exactPageList data :", this.exactPageList )   
this.paginationService.pageOnLoad();  
this.pageField = this.exactPageList; 

【讨论】:

以上是关于将exactPageList分配给pagefield时如何将其从any转换为any[]?的主要内容,如果未能解决你的问题,请参考以下文章

将'\'分配给char [重复]

将标签分配给不同的组分配方案

如何将 UIBarButtonSystemItemAction 图标分配给 UIButton?

如何将 TemplatedParent Content 属性分配给变量?

将 Func 分配给表达式,反之亦然

将列表元素分配给变量