0

I'm trying to rewrite my mobile apps using the latest Angular 6/Ionic 4. I just can't seem to get a basic forEach working. I'm trying all types of ways, but Sublime Text & Angular CLI throws errors. Please see the screenshot below. You can see the red squiggly lines in the Sublime Text Editor. The error is commented above each code block. Does anyone have any idea where I'm going wrong?

Screenshot of forEach Errors

Thanks for your time - please don't hesitate to let me know if you need additional info.

array = [1, 2, 3];

  // Duplicate identifier 'array'. -- Parameter declaration expected.
  array.forEach(function(element) {
    console.log(element);
  })

  // Unexpected token. A constructor, method, accessor, or property was expected.
  this.array.forEach((key : any, val: any) => {
    console.log(val);
  })

  // Cannot find name 'array'. Did you mean 'Array'?
  array.forEach((key : any, val: any) => {
    console.log(val);
  })

  // Cannot find name 'array'. Did you mean 'Array'?
  for (let number of array) {
   console.log(number);
  }
9
  • 1
    What if you do let array = [...]? Commented Oct 26, 2018 at 17:55
  • 1
    please add whole file, it seems it's not forEach problem Commented Oct 26, 2018 at 17:58
  • I'd look at some of the syntax here: typescriptlang.org/docs/handbook/iterators-and-generators.html number may be reserved? github.com/Microsoft/TypeScript/issues/2536 Commented Oct 26, 2018 at 17:58
  • Post your code as text. We can't copy and paste code from an image. Blind people can't read code in an image. Commented Oct 26, 2018 at 17:59
  • @JBNizet sorry - just wanted you to see the squigglys in the editor. Will post code next time. Commented Oct 26, 2018 at 18:04

2 Answers 2

3

You need to be inside methods to access component variables. Try your code in some methods or in constructor. Something like: constructor() { Your code }

Its not required that you should call it in constructor only, you can call the component variables through any methods by using this.componentVaribale inside a method. Obviously you can create local variables in methods using 'let' keyword!

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

4 Comments

Yup - needed to put it in the constructor, thanks so much. Will accept answer soon.
just to clarify, its not required that you should call it in constructor only, you can call the component variables through any methods by using this.componentVaribale inside a method. Obviously you can create local variables in methods using 'let' keyword!
@alokstar, you might want to add that to the answer.
@alokstar thanks for the clarification and your time.
0

Issue

You have issue with variable declaration scoping.

Fix

Change 1 : Local variable

let array = [1,2,3]

Change 2 : Accessing instance variable by this.

If you want to access this you must use inside the function.

function test(){
   this.array.forEach(function(){});
}

1 Comment

Thank you for your time!

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.