2

While trying to update a dictionary inside the iterator I got the error "SyntaxError: expected expression, got keyword 'if'"

The ut_frequencies data looks like:

{ "_id" : "app1", "cnt" : 3422 }
{ "_id" : "app2", "cnt" : 2752 }
{ "_id" : "app3", "cnt" : 2736 }
{ "_id" : "app4", "cnt" : 2711 }

Which I suppose is a list of dictionaries. I want to check if the app is already in the data dictionary and if so, update the "utf" value, if not, then add a new item only with the "utf" attribute set.

ut_frequencies.forEach(item=> 
  if(item._id in data){
    data[item._id]["utf"] = item.cnt;

  }else{
    data[item._id] = {utf: item.cnt, atf: 0, ptf: 0};
  }
)

Which is the best way to solve this.

1
  • You actually dont need the if/else yet. Commented Feb 6, 2018 at 13:48

1 Answer 1

2

The problem is that on the right of the arrow, an expression is expected.

An if condition is not an expression, therefore you should put braces around the body of the function.

ut_frequencies.forEach(item => {
    if(item._id in data){
        data[item._id]["utf"] = item.cnt;
    }else{
        data[item._id] = {utf: item.cnt, atf: 0, ptf: 0};
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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.