如何检查元组数组中的索引[0]中的值,如果存在匹配,则显示匹配元组的索引[1]处的值



我有一个元组数组:

var tuparray: [string, number][];
tuparray = [["0x123", 11], ["0x456", 7], ["0x789", 6]];
const addressmatch = tuparray.includes(manualAddress);

我希望我的函数检查tuparray是否包含用户输入(字符串),manualAddress。如果它在任何元组中找到匹配,它应该显示元组中[1]处的数字的值。

if (addressmatch){
console.log("address qualifies for [matched tuple number here]");

任何帮助在这将是非常感激!

Array上使用find()并提供lambda函数来计算匹配

var tuparray = [["0x123", 11], ["0x456", 7], ["0x789", 6]];
function includes(manualAddress) {
const found = tuparray.find(x => x[0] == manualAddress)

if (found) {
console.log(`address qualifies for ${found[1]}`)
} else {
console.log(`Not found`)
}
}
includes('0x123')
includes('0x456')
includes('0x111')

相关内容

最新更新