2

when I am initializing array globally (outside the method) its working fine. but when I am initializing the same array inside the method its throwing error unexpected token. you can see into code for location of array. this is array calculateResult[] = [];

Screenshot of the error

private log: string ='result';
private kw: string = 'kw';
private frame: number = 0;
public finalResult[] = [];

//here this array is working fine
calculateResult[] = [];

DisplayResult(){

  //if i initialize this array here, it's throwing error
  // calculateResult[] = [];

  if(some_conditions_true){
     alert();
     this.log = '1SE0 070-2NC70' '\n';
     this.kw = '.37' '\n';
     this.frame = '71' '\n';

     this.calculateResult[0] = this.log;
     this.calculateResult[1] = this.kw;
     this.calculateResult[2] = this.frame;

     this.finalResult.push(this.calculateResult);

     for(i=0;i < this.finalResult.length;i++){
       console.log(this.finalResult[0][0]);
       console.log(this.finalResult[0][1]);
     }
}

1
  • What are '\n' s for? Don't you have compiler error for that? Commented Aug 21, 2017 at 20:02

2 Answers 2

3

The first declaration is considered as a class property, so it's correct.

The second one, is incorrect because it is inside the class method and thus should be either declared as

let calculateResult=[];

if you intend to declare a new array of that name,

either addressed as the class property declared above as

this.calculateResult = ...

So keep the first one: calculateResult = [];

If you keep it commented, this.calculateResult[0] will be undefined in the class method, you cannot refer to it.

Sign up to request clarification or add additional context in comments.

6 Comments

@Faisal guys I tried both let calculateResult=[], let calculateResult [] = []; but I am getting error "14 caused by: Cannot set property '0' of undefined" i think because of this code. this.calculateResult[0] = this.log; this.calculateResult[1] = this.kw; this.calculateResult[2] = this.frame; ANY SUGGESTION ?
Uncomment calculateResult[] = [];, it's the correct one
You will need to post a new question, with the code and a possible plunker. It will be out of the scope of this question
just a quick question, I am able to access values in console this way "console.log(this.finalResult[i][0]);" how can i access these 2D values in template.is there any alternative? this way is throwing error {{finalResult[i][0]}} . one Dimension is working {{finalResult[i]}} .
In which context do you access finalResult[i][0] ? in an ngfor?
|
1

You should initialize as

 DisplayResult(){
    this.calculateResult = [];
 }

1 Comment

thanks now its not throwing error. but it is not updating the array. if I am calling the method again, its not updating the values instead its just append the same values again. any suggestion?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.