-2

enter image description here

I am trying filter the object and when I do it I am getting this error what actually it is saying I am not getting it.

Expected an identifier and instead saw 'let'.

This is my filter function

var arr = $scope.items; //object data
var stringToFilter = newSortingOrder.toString();
let obj = arr.find(o => o.id === stringToFilter); //error stopping in this line.
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Mr world wide
  • 4,696
  • 7
  • 43
  • 97
  • 4
    The `let` keyword requires a fairly up-to-date JavaScript environment. – Pointy Mar 28 '18 at 14:23
  • 2
    What browser are you running this on? You could just use var instead of let in line 3 – Patrick Hund Mar 28 '18 at 14:23
  • ES2015 isn't exactly a new thing - it's been supported by modern browsers for quite a while ago. Maybe OP is using IE – CertainPerformance Mar 28 '18 at 14:24
  • 2
    are you using jslint – Hussein Salman Mar 28 '18 at 14:24
  • 3
    if it doesn't like `let` then it definitely won't like `find(o => o.id === stringToFilter)` – phuzi Mar 28 '18 at 14:24
  • 2
    It would help if you bothered to say what environment this is.. code editor, browser/version, linter etc – Dominic Mar 28 '18 at 14:25
  • 1
    @Mrworldwide use ES5 syntax `var obj = arr.find(function(o) { return o.id === stringToFilter; });` – phuzi Mar 28 '18 at 14:25
  • Try replacing `let` with `var` as suggested in the first comment. – Pointy Mar 28 '18 at 14:26
  • That "Service Now" thing looks like it's providing a JavaScript environment based on a Java environment, so it could be some old version of Rhino. – Pointy Mar 28 '18 at 14:29
  • @igor it's just the line above but clipped by the image newSortingOrder.toString // clipped here – phuzi Mar 28 '18 at 14:29
  • If you've written all your code in ES6 but need ES5 support you could use babel. See: http://babeljs.io/repl/ – scagood Mar 28 '18 at 14:35
  • The message looks like a lint error. There are two ways to handle lint errors, eiher configure the linter so that it supports your JS version, or write the code according to the requirements set in the lint configuration. – Teemu Mar 28 '18 at 14:36

1 Answers1

2
let obj = arr.find(o => o.id === stringToFilter);

Is ES2015/ES6 (they're the same) syntax and, although it's not exactly new, not all environments will support it.

Use ES5 syntax instead to resolve your issue. It's much more widely supported.

var obj = arr.find(function(o) { return o.id === stringToFilter; });
phuzi
  • 12,078
  • 3
  • 26
  • 50