我有这个对象,我想让这样的用户不要将其他项添加到数据库中,当对象中有一个具有相同键名的项时。
{
"0360841d73bd74b268dcc3abad2555c0": {
"file_dislikes": 0,
"file_likes": 0,
"slang": "mmmmmmmmm",
"slangDefine": "dddddddddd",
"slangExample": "ddddddd",
"slangID": "0360841d73bd74b268dcc3abad2555c0",
"time_date": "2016-09-15T16:28:19.384Z",
"user_id": "0Uwx4oXQVEckArRSZNerwhfDZFF3"
},
"072794611d87924ac134bc87ae202c05": {
"file_dislikes": 0,
"file_likes": 0,
"slang": "damnit",
"slangDefine": "Damnit",
"slangExample": "Damnit",
"slangID": "072794611d87924ac134bc87ae202c05",
"time_date": "2016-09-14T13:40:47.745Z",
"user_id": "bHDKSU6aorX9efzBCHyVI4JBTh53"
}
}
我有对象vm.slangs
中的项目 for (var keys in vm.slangs) {
var getThisObject = vm.slangs[keys];
if ((getThisObject.slang).toLowerCase() === ("Damnit").toLowerCase()) {
console.log("there is a slang with that name!");
// Item cannot be added to database.
}
}
我认为这应该有效,但不是我做错了。
答案 0 :(得分:1)
如果找到俚语,你需要一个标志并退出循环并阻止插入数据库。
var unique = true;
for (var keys in vm.slangs) {
var getThisObject = vm.slangs[keys];
if (getThisObject.slang.toLowerCase() === "Damnit".toLowerCase()) {
console.log("there is a slang with that name!");
unique = false;
break;
}
}
if (unique) {
// add to db
}