我设置了 10 个“标签”,每个都有一个 ID(格式:00:00:00:xx:xx:xx)和 translatedID(格式:TXT)。我得到字符串列表违反标签,我想将其从 translatedID 转换为 ID。
我有一个定义值的映射:
Map(10) {
'00:00:00:02:28:47' => 'T7T',
'00:00:00:02:89:70' => 'T3T',
'00:00:00:02:89:51' => 'T4T',
'00:00:00:02:27:bd' => 'T5T',
'00:00:00:02:89:31' => 'T6T',
'00:00:00:02:89:a0' => 'T1T',
'00:00:00:02:89:af' => 'T2T',
'00:00:00:02:89:4d' => 'T9T',
'00:00:00:02:89:28' => 'T10T',
'00:00:00:02:89:a1' => 'T8T'
}
例子:
input = 'T4T____T5T____T2T____T10T____T6T____'
(desired) output = '00:00:00:02:89:51, 00:00:00:02:27:bd, 00:00:00:02:89:af, 00:00:00:02:89:28, 00:00:00:02:89:31'
我的第一个想法是遍历映射中的每个值,查看它是否存在于输入中,如果存在,则将相应的 ID 添加到输出中。但由于不必要的额外循环,这不是最优的。有什么建议么?
此外,我的代码使用全局变量来定义地图变量。我知道这是不受欢迎的,我有更好的方法吗?我的代码格式如下:
let dict = new Map()
function customQueryString() {
return new Promise((resolve, reject) => {
client.query(query, (err, res) => {
res.rows.forEach(psqltag => {
dict.set(psqltag.m_violator_tag, psqltag.m_translatedid)
});
resolve(queryStringAddition) // defined elsewhere doesnt matter for this problem
})
})
}
function loopingQueryFunction(loopQuery) {
client.query(loopQuery, (err, res) => {
res.rows.forEach(tag => {
input = tag.violating_tags // where the input string is found
output = ??? // where the translation needs to happen
}
}
}
async function asyncCall() {
let qStringAddition = await customQueryString();
loopQuery = loopQuery + qStringAddition
for (let i = 0; i< 100; i++) {
console.log("loop " + i)
await delay(1000)
loopingQueryFunction(loopQuery)
}
}
慕沐林林
相关分类