Alternative version for Object.values()

前端 未结 11 1727
花落未央
花落未央 2020-11-28 06:13

I\'m looking for an alternative version for the Object.values() function.
As described here the function is not supported in Internet Explorer.

When

11条回答
  •  Happy的楠姐
    2020-11-28 06:54

    Since Object is a (not so) recent implementation, if you want to support all browsers (AKA IE8 and below), then you need to create your own function:

    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-6 tag. Btw I keep this answer here, just in case someone needs it.

提交回复
热议问题