在试图更新迭代器中的字典时,我得到了错误"SyntaxError:预期表达式,got关键字'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};
}
)这是解决这个问题的最好方法。
发布于 2018-02-06 13:41:00
问题是,在箭头的右边,需要一个表达式。
if条件不是表达式,因此应该将大括号放在函数的主体上。
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};
}
});https://stackoverflow.com/questions/48644341
复制相似问题