Internet Explorer和ReactJS的Object.entries()替代 [英] Object.entries() alternative for Internet Explorer and ReactJS

查看:132
本文介绍了Internet Explorer和ReactJS的Object.entries()替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我已经在构建一个Web应用程序两周了,一切都很好。我已经到了我必须在Internet Explorer中测试的部分,并且所有出现的事情(除了一个之外,都已修复)不支持Object.entries()。

Well, I've been building a web application for a couple weeks now, and everything good. I got to the part that I had to test in Internet Explorer, and of all of the things that came up (all fixed except for one), Object.entries() is not supported.

我一直在做一些研究,试图提出一个简单的选择,但是一点都不运气。

I've been doing some research and try to come up with a simple alternative, but no luck at all.

更具体地说,我是m从API带来一个对象,以填充< select>< / select> 字段的选项,我必须过滤一些信息,就像这样:

To be more specific, I'm bringing an object from an API, to fill options for <select></select> fields I have to filter some information, just like this:

Object.entries(this.state.filterInfo.sectorId).map(this.eachOption)

// Function
eachOption = ([key, val], i) => {
    return(
        <option value={val} key={i}>{val}</option>
    );
}

因此,除了Internet Explorer之外,其他所有东西都可以正常工作。事实是,在这个特定组件中,我渲染了30个以上的< select>< / select> 字段。如果有不需要我重新构建所有内容的解决方案,那就太好了。

So everything works correctly except for Internet Explorer. The thing is that in this particular component I'm rendering over 30 <select></select> fields. IF there is a solution that doesn't require me to rebuild everything, it would be amazing.

有没有简单的解决方案?可以解决这个问题吗?

Is there a simple solution? A way around this?

预先感谢。

推荐答案

要在较旧的浏览器中使用较新的API时,通常要研究的第一项是是否有简单的polyfill。而且,可以肯定的是,在Object.entries()有一个非常简单的polyfill / docs / Web / JavaScript / Reference / Global_Objects / Object / entries#Polyfill rel = noreferrer> MDN文档网站:

The usual first item to research when you want to use a newer API in an older browser is whether there is a simple polyfill. And, sure enough there is a pretty simple polyfill for Object.entries() shown on the MDN doc site:

if (!Object.entries)
  Object.entries = function( obj ){
    var ownProps = Object.keys( obj ),
        i = ownProps.length,
        resArray = new Array(i); // preallocate the Array
    while (i--)
      resArray[i] = [ownProps[i], obj[ownProps[i]]];

    return resArray;
  };

这篇关于Internet Explorer和ReactJS的Object.entries()替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆