Javascript Object.values() 的替代版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42830257/
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
Alternative version for Object.values()
提问by user1170330
I'm looking for an alternative version for the Object.values()function.
As described herethe function is not supported in Internet Explorer.
我正在寻找该Object.values()功能的替代版本。
如此处所述,Internet Explorer 不支持该功能。
When executing the following example code:
执行以下示例代码时:
var obj = { foo: 'bar', baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]
It works in both, Firefox and Chrome, but throws the following error in IE11:
它适用于 Firefox 和 Chrome,但在 IE11 中会引发以下错误:
Object doesn't support property or method "values"
对象不支持属性或方法“值”
Here you can test it: Fiddle.
在这里你可以测试它:Fiddle。
So, what would be a quick fix?
那么,什么是快速修复?
回答by Nenad Vracar
You can get array of keys with Object.keys()and then use map()to get values.
您可以获取键数组,Object.keys()然后用于map()获取值。
var obj = { foo: 'bar', baz: 42 };
var values = Object.keys(obj).map(function(e) {
return obj[e]
})
console.log(values)
With ES6 you can write this in one line using arrow-functions.
使用 ES6,您可以使用箭头函数将其写在一行中。
var values = Object.keys(obj).map(e => obj[e])
回答by Chong Lip Phang
Object.values() is part of the ES8(June 2017) specification. Using Cordova, I realized Android 5.0 Webview doesn't support it. So, I did the following, creating the polyfill function only if the feature is not supported:
Object.values() 是 ES8(2017 年 6 月)规范的一部分。使用 Cordova,我意识到 Android 5.0 Webview 不支持它。因此,我执行了以下操作,仅在不支持该功能时才创建 polyfill 函数:
if (!Object.values) Object.values = o=>Object.keys(o).map(k=>o[k]);
回答by Jorge Fuentes González
Since Object is a (not so) recent implementation, if you want to support allbrowsers (AKA IE8 and below), then you need to create your own function:
由于 Object 是(并非如此)最近的实现,如果您想支持所有浏览器(AKA IE8 及以下),那么您需要创建自己的函数:
function objectValues(obj) {
var res = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
res.push(obj[i]);
}
}
return res;
}
PS: Just noticed the ecmascript-6tag. Btw I keep this answer here, just in case someone needs it.
PS:刚注意到ecmascript-6标签。顺便说一句,我把这个答案留在这里,以防万一有人需要它。
回答by Daniel Habenicht
回答by Alex Pánek
You can use a polyfill:
您可以使用 polyfill:
const valuesPolyfill = function values (object) {
return Object.keys(object).map(key => object[key]);
};
const values = Object.values || valuesPolyfill;
console.log(values({ a: 1, b: 2, c: 3 }));
回答by Antikhippe
For people using UnderscoreJS, you can get object values by using _.values:
对于使用UnderscoreJS 的人,您可以使用_.values以下方法获取对象值:
var obj = { foo: 'bar', baz: 42 };
console.log(_.values(obj)); // ['bar', 42]
回答by Aamin Khan
var x = {Name: 'John', Age: 30, City: 'Bangalore'};
Object.prototype.values = function(obj) {
var res = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
res.push(obj[i]);
}
}
return res;
};
document.getElementById("tag").innerHTML = Object.values(x)
<p id="tag"></p>
回答by Novy
I know it is a old topic. I was playing arround and just want to add another implementation. It is simply the map version with the map itself implemented with a reduce :
我知道这是一个古老的话题。我正在玩,只想添加另一个实现。它只是地图版本,地图本身使用 reduce 实现:
let obj = { foo: 'bar', baz: 42 };
const valueArray = Object.keys(obj).reduce((acc, key) => {
acc.push(obj[key]);
return acc;
}, []);
console.log(valueArray);
It does the job but it has something that bother me. The reducer function uses objand the objwas not injectedin it. We should avoid global variableinside functions and make our functions more testable. So I do prefer this version with a reducer function helperthat takes the obj as parameter an return the actual reducer function. It becomes:
它可以完成工作,但它有一些困扰我的东西。reducer 函数使用obj并且obj没有注入其中。我们应该避免在函数内部使用全局变量,并使我们的函数更具可测试性。所以我更喜欢这个版本的reducer 函数助手,它将 obj 作为参数并返回实际的 reducer 函数。它成为了:
let obj = { foo: 'bar', baz: 42 };
// reducer function helper take obj and return the actual reducer function
let reducerFuncHelper= obj => (acc, key) => {
acc.push(obj[key]);
return acc;
}
//much better
const valueArray = Object.keys(obj).reduce(reducerFuncHelper(obj), []);
console.log(valueArray);
回答by Plxmax
Best way is to replace it with the values method of ramda:
最好的方法是用 ramda 的 values 方法替换它:
import * as R from 'ramda';
const obj = { foo: 'bar', test: 10 };
console.log(R.values(obj)) // ['bar', 10]
回答by Frank34
As I didn't found an answer for my needs:
由于我没有找到满足我需求的答案:
var obj = { foo: 'bar', baz: 42 };
console.log(obj[Object.keys(obj)[0]]) // bar
console.log(obj[Object.keys(obj)[1]]) // 42
Using the possibility of objects to adress them per literal.
使用对象的可能性来按文字寻址它们。

