简体   繁体   English

Object.values() 的替代版本

[英]Alternative version for Object.values()

I'm looking for an alternative version for the Object.values() function.我正在寻找Object.values()函数的替代版本。
As described here the function is not supported in Internet Explorer.如此处所述,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?那么,什么是快速解决方案?

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])

Object.values() is part of the ES8(June 2017) specification. Object.values() 是 ES8(2017 年 6 月)规范的一部分。 Using Cordova, I realized Android 5.0 Webview doesn't support it.使用 Cordova,我意识到 Android 5.0 Webview 不支持它。 So, I did the following, creating the polyfill function only if the feature is not supported:因此,我执行了以下操作,仅在不支持该功能时才创建 polyfill 函数:

if (!Object.values) Object.values = o=>Object.keys(o).map(k=>o[k]);

Since Object is a ( not so ) recent implementation, if you want to support all browsers (AKA IE11 and below), then you need to create your own function:由于 Object 是(不是)最近的实现,如果您想支持所有浏览器(AKA IE11 及以下),那么您需要创建自己的函数:

function objectValues(obj) {
    var res = [];
    for (var i in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, i)) {
            res.push(obj[i]);
        }
    }
    return res;
}

You can also modify this for Object.keys() and Object.entries() easily.您还可以轻松地为Object.keys()Object.entries()修改它。

PS: Just noticed the ecmascript-6 tag. PS:刚刚注意到ecmascript-6标签。 Btw I keep this answer here, just in case someone needs it.顺便说一句,我把这个答案放在这里,以防有人需要。

如果您已经在使用core-js (例如使用 Angular),您可以导入相应的 polyfill:

   import 'core-js/es7/object';

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 }));

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]

 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>

I know it is a old topic.我知道这是一个老话题。 I was playing arround and just want to add another implementation.我在玩arround,只想添加另一个实现。 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 obj and the obj was not injected in it. reducer 函数使用obj并且obj没有注入其中。 We should avoid global variable inside functions and make our functions more testable .我们应该避免函数内部的全局变量,并使我们的函数更可测试 So I do prefer this version with a reducer function helper that takes the obj as parameter an return the actual reducer function .所以我更喜欢这个带有减速器函数帮助器的版本,它将 obj 作为参数并返回实际的减速器函数 It becomes:它成为了:

 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);

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.使用对象的可能性来按字面量对它们进行寻址。

You can get array of keys using Object.keys().您可以使用 Object.keys() 获取键数组。 This will work in IE also.这也适用于 IE。 Object.values() is not required at all to get values since we can use the keys obtained from Object.keys() to get values as below: Object.values() 根本不需要获取值,因为我们可以使用从 Object.keys() 获得的键来获取值,如下所示:

var obj = { foo: 'bar', baz: 42 };
var keyArray = Object.keys(obj);
for(var i = 0; i < keyArray.length; i++)
    console.log(obj[keyArray[i]]); 

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM