我遇到了以下问题。
它与位于variables1.service.ts文件中的数组连接。我已经创建了一个函数,它应该产生一个数组,结果是其他两个数组元素的乘法。我找了一个打字稿的例子,但我想知道它是否正确。我不是程序员,但是我想知道.您能检查下面的代码是否有效吗?
variables1.service.ts
import { Injectable } from "@angular/core";
@Injectable({
providedIn:'root'
})
export class Variables1Service{
Fcomponent: number[]=[0,1,2,3,4,5,6,7,8,9,10];
Scomponent: number[]=[0,1,2,3,4,5,6,7,8,9,10];//代码,它创建问题-启动
createmulti(array1,array2){
let array3:number[]=[];
for (let i1 = 0; array1.length-1; i1++) {
for (let i2 = 0; i2 < array2.length-1; i2++) {
array3.push(array1[i1]*array2[i2]);
}
}
return array3;
}
public Amulti = this.createmulti(this.Fcomponent,this.Scomponent);//创建问题端的代码
F1comp: number[]=this.Fcomponent;
S1comp: number[]=this.Scomponent;
shuffle(array){
for (let i=array.length-1;i>0;i--) {
let j=Math.floor(Math.random()*(i+1));
[array[i],array[j]]=[array[j],array[i]];
}
return array;
}
public F1component = this.shuffle(this.F1comp);
public S1component = this.shuffle(this.S1comp);项目已成功编译,但当我试图在浏览器中查看它时:http://localhost:4200/会出现内存不足的消息。
怎样才能解决这个问题呢?我的代码正确吗?
发布于 2021-05-05 19:50:29
您的错误是在createMulti中的for循环中,在外部循环中没有条件。你需要i1 < array1.length,你刚刚拥有了array1.length-1。
// for (let i1 = 0; array1.length-1; i1++) {
for (let i1 = 0; i1 < array1.length; i1++)
for (let i2 = 0; i2 < array2.length; i2++) {
array3.push(array1[i1]*array2[i2]);
}
}此外,通过执行i2 < array2.length - 1,您将省略最后一个元素。我在上面的例子中修改了它。
https://stackoverflow.com/questions/67407673
复制相似问题