IE11 compatible Object.fromEntries

Viewed 920

So I'm looking to make my ES6 JavaScript code compatible with IE11, but I'm having some issues.

Here is the return for var res inside Google Chrome: enter image description here

Tip: category is the key, result[category] is the value.

Here is the code that I have:

for (var category in result) {
    var res = Object.fromEntries(Object.keys(result[category]).filter(x => x !== 'Other').concat('Other').map(x => [x, result[category][x]]));
    console.log(res);
}

Could someone guide me in the right direction on how I would be able to make var res compatible with IE11? Looks like Object.fromEntries and Object.keys might not be supported.

1 Answers

You can polyfill Object.fromEntries using a simple for loop.

function fromEntries(entries){
    var res = {};
    for(var i = 0; i < entries.length; i++) res[entries[i][0]] = entries[i][1];
    return res;
}
if(!Object.fromEntries) Object.fromEntries = fromEntries;
console.log(fromEntries([['a', 1], ['b', 2]]))

Related