Javascript 打字稿中的对象长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38074410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:02:58  来源:igfitidea点击:

Object length in typescript?

javascripttypescriptangular

提问by Bhushan Gadekar

is there any way in typescript by which I can get length of an object:

在打字稿中有什么方法可以让我得到一个对象的长度:

Something like this:

像这样的东西:

say I have an object:

说我有一个对象:

public customer:any={
     "name":"Bhushan",
     "eid":"879546",
     "dept":"IT"
}

Now I am trying to get its length in typescript.

现在我试图在打字稿中获得它的长度。

ie. when I am doing customer.length(), I should be able to get value 3 as it has 3 elements.

IE。当我这样做时customer.length(),我应该能够获得价值 3,因为它有 3 个元素。

I tried Object.getOwnPropertyNames(customer.value)but its returning 2 whereas I have 3 elements in my object.

我试过,Object.getOwnPropertyNames(customer.value)但它返回 2 而我的对象中有 3 个元素。

any inputs?

任何输入?

回答by Thierry Templier

You could try the following:

您可以尝试以下操作:

Object.keys(customer).length

回答by Ankit Singh

Object.keys(this.customer).length