typescript 如何在打字稿中使用 Object.values?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42966362/
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
How to use Object.values with typescript?
提问by user2280016
I am trying to form a comma separated string from an object,
我试图从一个对象形成一个逗号分隔的字符串,
const data = {"Ticket-1.pdf":"8e6e8255-a6e9-4626-9606-4cd255055f71.pdf","Ticket-2.pdf":"106c3613-d976-4331-ab0c-d581576e7ca1.pdf"};
const values = Object.values(data).map(x => x.substr(0, x.length - 4));
const commaJoinedValues = values.join(',');
console.log(commaJoinedValues);
How to do this with TypeScript?
如何使用 TypeScript 做到这一点?
getting an error file:
获取错误文件:
severity: 'Error'
message: 'Property 'values' does not exist on type 'ObjectConstructor'.'
at: '216,27'
source: 'ts'
采纳答案by holi-java
using Object.keys instead.
使用 Object.keys 代替。
const data = {
a: "first",
b: "second",
};
const values = Object.keys(data).map(key => data[key]);
const commaJoinedValues = values.join(",");
console.log(commaJoinedValues);
回答by Aaron Beall
Object.values()
is part of ES2017, and the compile error you are getting is because you need to configure TS to use the ES2017 library. You are probably using ES6 or ES5 library in your current TS configuration.
Object.values()
是ES2017 的一部分,你得到的编译错误是因为你需要配置 TS 才能使用 ES2017 库。您可能在当前的 TS 配置中使用 ES6 或 ES5 库。
Solution: use es2017
or es2017.object
in your --lib
compiler option.
解决方案:在您的编译器选项中使用es2017
或。es2017.object
--lib
For example, using tsconfig.json
:
例如,使用tsconfig.json
:
"compilerOptions": {
"lib": ["es2017", "dom"]
}
Note that targeting ES2017 with TypeScript does notemit polyfills in the browser for ES2017(meaning the above solves your compileerror, but you can still encounter a runtimeerror because the browser doesn't implement ES2017 Object.values
), it's up to you to polyfill your project code yourself if you want. And since Object.values
is not yet well supported by all browsers(at the time of this writing) you definitely want a polyfill: core-js
will do the job.
需要注意的是针对ES2017以打字稿并没有发出polyfills在浏览器ES2017(指上述解决您的编译错误,但你仍然可以遇到一个运行时错误,因为该浏览器没有实现ES2017 Object.values
),它是由你来填充工具项目如果你愿意,自己编码。由于Object.values
尚未得到所有浏览器的充分支持(在撰写本文时),您肯定需要一个 polyfill:core-js
将完成这项工作。
回答by mtpultz
You can use Object.values
in TypeScript by doing this (<any>Object).values(data)
if for some reason you can't update to ES7 in tsconfig.
如果由于某种原因您无法在 tsconfig 中更新到 ES7,您可以Object.values
通过执行此操作在 TypeScript中使用(<any>Object).values(data)
。
回答by Kian
Instead of
代替
Object.values(myObject);
use
用
Object["values"](myObject);
In your example case:
在您的示例中:
const values = Object["values"](data).map(x => x.substr(0, x.length - 4));
This will hide the ts compiler error.
这将隐藏 ts 编译器错误。
回答by Lukas Ignatavi?ius
I have increased target in my tsconfig.json
to enable this feature in TypeScript
我增加了目标以tsconfig.json
在 TypeScript 中启用此功能
{
"compilerOptions": {
"target": "es2017",
......
}
}
回答by Neil Stevens
I just hit this exact issue with Angular 6 using the CLI and workspaces to create a library using ng g library foo
.
我刚刚使用 CLI 和工作区在 Angular 6 中遇到了这个确切的问题,以使用ng g library foo
.
In my case the issue was in the tsconfig.lib.json
in the library folder which did not have es2017
included in the lib
section.
在我的情况下,问题出tsconfig.lib.json
在库文件夹中,该文件夹未es2017
包含在该lib
部分中。
Anyone stumbling across this issue with Angular 6 you just need to ensure that you update you tsconfig.lib.json
as well as your application tsconfig.json
任何在 Angular 6 中遇到此问题的人,您只需要确保更新您tsconfig.lib.json
和您的应用程序tsconfig.json
回答by kuncevic.dev
Having my tslint
rules configuration here always replacing the line Object["values"](myObject)
with Object.values(myObject)
.
有我的tslint
规则配置在这里总是更换线Object["values"](myObject)
带Object.values(myObject)
。
Two options if you have same issue:
如果你有同样的问题,两种选择:
(Object as any).values(myObject)
(Object as any).values(myObject)
or
或者
/*tslint:disable:no-string-literal*/
`Object["values"](myObject)`
回答by Maksymilian Majer
Simplest way is to cast the Object
to any
, like this:
最简单的方法是将 to 强制Object
转换any
,如下所示:
const data = {"Ticket-1.pdf":"8e6e8255-a6e9-4626-9606-4cd255055f71.pdf","Ticket-2.pdf":"106c3613-d976-4331-ab0c-d581576e7ca1.pdf"};
const obj = <any>Object;
const values = obj.values(data).map(x => x.substr(0, x.length - 4));
const commaJoinedValues = values.join(',');
console.log(commaJoinedValues);
And voila – no compilation errors ;)
瞧——没有编译错误;)
回答by Lucas
Even simpler, use _.values
from underscore.js
https://underscorejs.org/#values
更简单,_.values
从 underscore.js
https://underscorejs.org/#values 使用