【发布时间】:2018-07-16 14:15:47
【问题描述】:
在尝试更新迭代器中的字典时,我收到错误“SyntaxError: expected expression, got keyword 'if'”
ut_frequencies 数据如下所示:
{ "_id" : "app1", "cnt" : 3422 }
{ "_id" : "app2", "cnt" : 2752 }
{ "_id" : "app3", "cnt" : 2736 }
{ "_id" : "app4", "cnt" : 2711 }
我想这是一个字典列表。我想检查应用程序是否已经在数据字典中,如果是,则更新“utf”值,如果没有,则仅添加一个设置了“utf”属性的新项目。
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};
}
)
解决这个问题的最佳方法是什么。
【问题讨论】:
-
你实际上还不需要 if/else。
标签: javascript loops if-statement foreach syntax-error