未捕获的类型错误:Object.values 不是函数 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38748445/
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
Uncaught TypeError: Object.values is not a function JavaScript
提问by Alex Fallenstedt
I have a simple object like the one below:
我有一个简单的对象,如下所示:
var countries = {
"Argentina":1,
"Canada":2,
"Egypt":1,
};
I need to create two arrays. The first array is an array of all the keys from the object. I created this array by:
我需要创建两个数组。第一个数组是对象中所有键的数组。我通过以下方式创建了这个数组:
var labels = Object.keys(countries);
This works well. I obtain an array of countries. Now when I try to create an array from the values...
这很好用。我获得了一系列国家。现在,当我尝试从值创建数组时...
var labels = Object.values(countries);
I get this error: Uncaught TypeError: Object.values is not a function JavaScript
我收到此错误: Uncaught TypeError: Object.values is not a function JavaScript
I don't know what I am doing wrong. I console.log countries
before and after I declare labels
and the object remains the same. How do I properly use Object.values()
?
我不知道我做错了什么。Iconsole.log countries
在声明之前和之后labels
,对象保持不变。我该如何正确使用Object.values()
?
回答by tymeJV
.values
is unsupported in many browsers - you can use .map
to get an array of all the values:
.values
在许多浏览器中不受支持 - 您可以使用它.map
来获取所有值的数组:
var vals = Object.keys(countries).map(function(key) {
return countries[key];
});
See MDN doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valuesor Official doc: https://tc39.github.io/ecma262/#sec-object.values(thanks @evolutionxbox for correction)
请参阅 MDN 文档:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values或官方文档:https: //tc39.github.io/ecma262/#sec- object.values(感谢@evolutionxbox 的更正)
回答by phobos
It's also worth noting that only Node versions >= 7.0.0 fully support this.
还值得注意的是,只有 Node 版本 >= 7.0.0 完全支持这一点。
回答by j3ff
For those who ended up here and are using Angular, adding import 'core-js/es7/object';
to polyfills.ts
file solved the problem for me.
对于那些最终来到这里并使用 Angular 的人来说,添加import 'core-js/es7/object';
到polyfills.ts
文件为我解决了这个问题。
/** IE9, IE10 and IE11 requires all of the following polyfills. **/
import 'core-js/es6/array';
import 'core-js/es6/date';
import 'core-js/es6/function';
import 'core-js/es6/map';
import 'core-js/es6/math';
import 'core-js/es6/number';
import 'core-js/es6/object';
import 'core-js/es6/parse-float';
import 'core-js/es6/parse-int';
import 'core-js/es6/regexp';
import 'core-js/es6/set';
import 'core-js/es6/string';
import 'core-js/es6/symbol';
import 'core-js/es6/weak-map';
import 'core-js/es7/array';
import 'core-js/es7/object'; // added import
回答by Venkata
Looks like this issue is fixed in Safari latest version. I came around the same issue. This issue occurs in browser version 9.0.1 and does not occur in 10.1.1
看起来这个问题已在 Safari 最新版本中修复。我遇到了同样的问题。此问题在浏览器版本 9.0.1 中出现,在 10.1.1 中不会出现
Editing to add the attachments:
编辑以添加附件:
[snippet][1]
[object value][2]
[browser version][3]
回答by Manohar Reddy Poreddy
Using "for...in" as discussed at mozilla: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values
使用在 mozilla 中讨论的“for...in”:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values
Here is the code I used:
这是我使用的代码:
function objectValues(obj) {
let vals = [];
for (const prop in obj) {
vals.push(obj[prop]);
}
return vals;
}
// usage
let obj = {k1: 'v1', k2: 'v1', k3: 'v1'};
console.log(objectValues(obj)); // prints the array [ 'v1', 'v1', 'v1' ]
// OR
console.log(objectValues(obj).join(', ')); // prints the string 'v1, v1, v1'
回答by KARTHIKEYAN.A
I think issue in compilation support on browsers compatibility, You can use mapto achieve the same.
我认为浏览器兼容性的编译支持问题,您可以使用map来实现相同的功能。
var countries = [
{
"Argentina": 1,
"Canada": 2,
"Egypt": 1,
"india": 1
},
{
"Argentina": 1,
"india": 1,
"US": 2,
"UK": 1,
}
];
var unpick = countries.map(d=>{ return Object.keys(d) });
console.log(unpick)
var countries = [
{
"Argentina": 1,
"Canada": 2,
"Egypt": 1,
"india": 1
},
{
"Argentina": 1,
"india": 1,
"US": 2,
"UK": 1,
}
];
var unpick = countries.map(d=>{ return Object.values(d) });
console.log(unpick)