未捕获的TypeError:Object.values不是函数JavaScript [英] Uncaught TypeError: Object.values is not a function JavaScript
问题描述
我有一个简单的对象,如下所示:
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);
我收到此错误:未捕获TypeError:Object.values不是函数JavaScript
我不知道我做错了什么。我在声明标签
之前和我的console.log 国家
之后,对象保持不变。如何正确使用 Object.values()
?
I don't know what I am doing wrong. I console.log countries
before I declare labels
and after and the object remains the same. How do I properly use Object.values()
?
推荐答案
.values
在许多浏览器中都不受支持 - 你可以使用 .map
获取所有值的数组:
.values
is unsupported in many browsers - you can use .map
to get an array of all the values:
var vals = Object.keys(countries).map(function(key) {
return countries[key];
});
请参阅MDN doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values 或Official doc: https://tc39.github.io/ecma262/#sec-object.values (感谢@evolutionxbox进行更正)
See MDN doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values or Official doc: https://tc39.github.io/ecma262/#sec-object.values (thanks @evolutionxbox for correction)
这篇关于未捕获的TypeError:Object.values不是函数JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!