有字典界面:
interface Dictionary<T> {
[Key: string]: T;
}
还有一些班级,每天都在重复一些健身运动:
export class ClassName {
DailyReapeatCount: Dictionary<number> = {};
}
让我们初始化它:
let myClass = new ClassName();
myClass.DailyReapeatCount['exersize_01'] = 3;
myClass.DailyReapeatCount['exersize_02'] = 4;
// ... and so on
然后我需要检查DailyReapeatCount是否大于5来进行锻炼:
// pseudo code there
let isAnyExist = myClass.DailyReapeatCount.some(p => p.value > 5);
我收到ts通知:
无法调用类型缺少调用签名的表达式。类型 该号码没有兼容的呼叫签名。“
如何获取此查询?
答案 0 :(得分:0)
使用标准Map(与C#中的Dictionary
相同):
const dictionary = new Map<string, number>([
["one", 1],
["two", 2],
["five", 5],
]);
const values = Array.from(dictionary.values());
const isExist = values.some(v => v < 5);
console.log(isExist);
// In addition you can do dictionary-based things like:
console.log(dictionary.has("five")); // check is key exists