1

I have the following code that basically gets some JSON data, looks for the keys with "servergenre", and saves the results in an array. This is a follow up of this question.

let result = [];
        Object.keys(data).forEach( key => {
            if(/servergenre/.test(key)){
                result.push(data[key])
            }
        });

Even though the code is working correctly, in some editors it raises syntactic errors:

  • "key": unsolvable variable or type key
  • "=>": expression expected
  • "if( / server...": formal parameter name expected
  • ")){": , expected
  • "});": statement expected

Here is an image to show you where the errors are:

screenshot of code with error underlines

As I said the code is working fine, I just need it to be fixed or another approach to get rid of the errors.

Furthermore, many compressors and minifiers do not support this bit of code. So I can’t minify it.

Thanks in advance.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
DannyBoy
  • 434
  • 5
  • 21
  • 1
    The editors have to allow ES6 – mplungjan Jun 16 '17 at 20:47
  • I do not know about ES6 but, I cannot compress or minify the code because of these errors – DannyBoy Jun 16 '17 at 20:57
  • 2
    You'll have to use a transpiler/minifier that supports ES6 syntax, such as Babel. – mhodges Jun 16 '17 at 20:58
  • 1
    Se her how to set the language level in IntelliJ https://stackoverflow.com/questions/35772101/phpstorm-let-definition-are-not-supported-by-current-javascript-version/35772193#35772193 – baao Jun 16 '17 at 20:58
  • @mhodges thanks it solved my issue. you can submit it as an answer so i can approve it. – DannyBoy Jun 16 '17 at 21:30
  • 1
    Rory O'Kane got it. I wouldn't really have anything else to add. You can mark his answer as accepted if it was the solution you're looking for. Glad we could help =) – mhodges Jun 16 '17 at 21:33

2 Answers2

2

ES2015, formerly known as ES6, is a more recent version of JavaScript, which introduces features such as the => syntax for functions you are using.

Not all features of ES2015 are fully supported in all browsers, so many people who use it pass it through a compiler (“transpiler”) first to convert it to ES5, which is supported by all browsers. Babel is one such transpiler. But if you are only targeting newer browsers, that would explain why the => syntax is working for you.

You just need to change your editor settings to understand that syntax. Exactly how you do that depends on what text editor you are using. It could be that your editor's built-in JavaScript mode doesn't know how to read ES2015 syntax, and you need to either upgrade your editor or install a third-party plugin that provides an updated error-checker. Or it could be that your editor supports both ES5 and ES2015, and it thinks you are trying to write your project only in ES5. In this case you just need to go to the settings and tell your editor that you mean for this project to use ES2015 (or ES2016, which is currently the most recent version).

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
1

Fat arrows are ES6 syntax. If that causes trouble, just write good old ES5 :

let result = [];
Object.keys(data).forEach( function(key) {
      if(/servergenre/.test(key)){
           result.push(data[key])
      }
});
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63