gpt4 book ai didi

javascript - 如何检查 javascript 对象键中的等效项

转载 作者:行者123 更新时间:2023-12-03 06:06:31 24 4
gpt4 key购买 nike

我有这个对象,我想让这样的用户在对象中存在具有相同键名的项目时不要向数据库添加其他项目。

{
"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.
}
}

我认为这应该可行,但事实并非如此,也许我做错了什么。

最佳答案

如果找到俚语并阻止插入数据库,您需要一个标志并退出循环。

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
}

关于javascript - 如何检查 javascript 对象键中的等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39516390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com