Typescript记录项目数

问题描述 投票:0回答:1

是否可以找到打字稿记录中的项目数?

例如类似

const testRecord: Record<string, string> = {
  'one': 'value1',
  'two': 'value2'
};

var length = testRecord.length;

// looking for length to be 2 but is undefined as there is no length property

供参考:https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkt

typescript2.0 typescript-generics
1个回答
0
投票

我刚刚在这里找到了一个javascript对象的长度的答案,似乎很不错:

Length of a JavaScript object

我回答上面示例的实现是:

const testRecord: Record<string, string> = {
    'one': 'value1',
    'two': 'value2'
  };

var length: Object.keys(testRecord).length;
// length equals 2

但是请告诉我是否有更好,更具体的“记录”方法来执行此操作?

© www.soinside.com 2019 - 2024. All rights reserved.