数组分割成数组嵌套对象 (常用于分页显示数据)
//模拟后台调取到的数据 public datas: any = [ {name: 1}, {name: 1}, {name: 1}, {name: 2}, {name: 2}, {name: 2}, {name: 3}, {name: 3}, {name: 3}, {name: 4}, {name: 4}, {name: 4}, {name: 5}, {name: 5}, {name: 5}, ]; public pages: any = []; //转化为对象存放在pages数组中 rows(pagesNum) { //在原型上创建一个方法 (Array as any).prototype.chunk = function (n) { for (var i = 0, temp = [], l = ~~this.length / n; temp.length < l; temp[i++] = this.splice(0, n)) ; return temp; }; //将获取的数据转化之后赋值给this.oages数组 this.pages= this.datas.chunk(9); //----------------------------------------------------- //通过传参将this.pages中下标(pagesNum)对应的对象内容打印出来 let arr = this.pages[pagesNum]; console.log(arr) }