gpt4 book ai didi

javascript - 查找不带逗号的字符串

转载 作者:行者123 更新时间:2023-11-30 07:20:49 24 4
gpt4 key购买 nike

我有这段代码,我在其中找到字符串值用逗号分隔的键

var db = {
"name": "Nkosana",
"middle": "Baryy",
"surname": "walked",
"batch_number": "test,b",
"temp": ",,67,6,87,86,5,67865,876,67",
"integrity": ",,,,,,,,,,,,,,,,,,,,,,,",
"weight": "760,765,755,758,759,758,758,769,758,762,759,7",
"comment": "oh la la",
};

for (var key in db) {
if (db.hasOwnProperty(key)) {
if(db[key].indexOf(',')>-1){
console.log(key+' | ' + db[key]);
}
}
}

如你所见,我可以检索这些值

batch_number | test,b
temp | ,,67,6,87,86,5,67865,876,67
integrity | ,,,,,,,,,,,,,,,,,,,,,,,
weight | 760,765,755,758,759,758,758,769,758,762,759,7

现在我想知道如果有一种方法可以检索没有逗号的值的键

最佳答案

indexOf(',') > -1 更改为 indexOf(',') == -1:

var db = {
"name": "Nkosana",
"middle": "Baryy",
"surname": "walked",
"batch_number": "test,b",
"temp": ",,67,6,87,86,5,67865,876,67",
"integrity": ",,,,,,,,,,,,,,,,,,,,,,,",
"weight": "760,765,755,758,759,758,758,769,758,762,759,7",
"comment": "oh la la",
};

for (var key in db) {
if (db.hasOwnProperty(key)) {
if(db[key].indexOf(',')==-1){
console.log(key+' | ' + db[key]);
}
}
}

关于javascript - 查找不带逗号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45238348/

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