简体   繁体   English

我如何在打字稿中使用字典查询linq?

[英]How do I linq like query with Dictionary in typescript?

There is interface of dictionary: 有字典的界面:

interface Dictionary<T> {
    [Key: string]: T;
}

And some class that counts daily repeating of some exersizes: 而有些班级每天都在重复一些健身运动:

export class ClassName {
   DailyReapeatCount: Dictionary<number> = {};
}

Let's initialize it: 让我们初始化它:

let myClass = new ClassName();
        myClass.DailyReapeatCount['exersize_01'] = 3;
        myClass.DailyReapeatCount['exersize_02'] = 4;
        // ... and so on

Then I need to check if it has any exersize with DailyReapeatCount more then 5: 然后,我需要检查DailyReapeatCount是否大于5来进行扩展:

// pseudo code there
let isAnyExist = myClass.DailyReapeatCount.some(p => p.value > 5);

There I get ts notification: 那里我得到ts通知:

Cannot invoke an expression whose type lacks a call signature. 无法调用类型缺少调用签名的表达式。 Type Number has no compatible call signatures." 类型号码没有兼容的呼叫签名。”

How do I get this query? 如何获得此查询?

Use standard Map (it's the same as Dictionary in C#): 使用标准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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM