Javascript 如何使TypeScript Array.map()返回与VanillaJS相同的值?
在我的Angular6应用程序中,我正在制作康威的生活游戏。我试图生成一个nxm二维类实例数组。在vanillaJS中,我将其作为:Javascript 如何使TypeScript Array.map()返回与VanillaJS相同的值?,javascript,arrays,typescript,Javascript,Arrays,Typescript,在我的Angular6应用程序中,我正在制作康威的生活游戏。我试图生成一个nxm二维类实例数组。在vanillaJS中,我将其作为: generateInitialState(bias) { return [...Array(this.rows)] .map((a, i) => [...Array(this.columns)] .map((b, j) => new Cell(j, i, (Math.round(Math.random()
generateInitialState(bias) {
return [...Array(this.rows)]
.map((a, i) => [...Array(this.columns)]
.map((b, j) => new Cell(j, i, (Math.round(Math.random() * 1000) < (1000 * bias)) ? 'alive' : 'dead')));
}
在vanillaJS中,这可以很好地工作,并根据需要生成数组。但是,Typescript中的上述代码仅返回长度为this.rows的空数组。
TypeScript似乎将其编译为:
function generateInitialState(bias) {
var _this = this;
return Array(this.rows).slice().map(function (a, i) { return Array(_this.columns).slice().map(function (b, j) { return new Cell(j, i, (Math.round(Math.random() * 1000) < (1000 * bias)) ? 'alive' : 'dead'); }); });
}
函数生成初始状态(偏差){
var_this=这个;
返回数组(this.rows.slice().map(函数(a,i){return Array(_this.columns.slice().map)(函数(b,j){return new Cell(j,i,(Math.round(Math.random()*1000)<(1000*bias))?'alive':'dead');});
}
我怎样才能让它在TypeScript中工作
完整代码
class Game {
constructor(columns, rows, randomBias){
this.columns = columns;
this.rows = rows;
this.randomBias = randomBias;
this.cells = this.generateInitialState(this.randomBias);
}
/* Content omitted for brevity */
generateInitialState(bias) {
return [...Array(this.rows)]
.map((a, i) => [...Array(this.columns)]
.map((b, j) => new Cell(j, i, (Math.round(Math.random() * 1000) < (1000 * bias)) ? 'alive' : 'dead')));
}
}
class Cell{
constructor(x, y, state){
this.x = x;
this.y = y;
this.state = state;
}
}
let a = new Game(4, 4, 0.5);
console.log(a.cells);
类游戏{
构造函数(列、行、随机偏差){
this.columns=列;
this.rows=行;
this.randomBias=randomBias;
this.cells=this.generateInitialState(this.randomBias);
}
/*为简洁起见省略了内容*/
发电机初始状态(偏差){
返回[…数组(this.rows)]
.map((a,i)=>[…数组(this.columns)]
.map((b,j)=>新单元格(j,i,(Math.round(Math.random()*1000)<(1000*bias))?“live”:“dead”);
}
}
类单元{
构造函数(x,y,state){
这个.x=x;
这个。y=y;
this.state=状态;
}
}
设a=新游戏(4,4,0.5);
console.log(a.cells);
问题在于如何初始化指定大小的数组。执行此操作时:
[...Array(this.rows)]
它被编译为数组(this.rows).slice()
,该数组不产生任何值,因为数组中填充了“洞”,这与原始(未编译)版本中填充了未定义的值的数组不同。map
不处理孔
请尝试Array.from({length:this.rows})
:
function generateInitialState(bias) {
return Array.from({ length: this.rows })
.map((a, i) => Array.from({ length: this.columns })
.map((b, j) => new Cell(j, i, (Math.round(Math.random() * 1000) < (1000 * bias)) ? 'alive' : 'dead')));
}
函数生成初始状态(偏差){
返回数组.from({length:this.rows})
.map((a,i)=>Array.from({length:this.columns})
.map((b,j)=>新单元格(j,i,(Math.round(Math.random()*1000)<(1000*bias))?“live”:“dead”);
}
function generateInitialState(bias) {
return Array.from({ length: this.rows })
.map((a, i) => Array.from({ length: this.columns })
.map((b, j) => new Cell(j, i, (Math.round(Math.random() * 1000) < (1000 * bias)) ? 'alive' : 'dead')));
}