Javascript 对 Object.entries 的 Babel 支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35090153/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:16:04  来源:igfitidea点击:

Babel support for Object.entries

javascriptbabeljsecmascript-next

提问by damd

I'm looking at the stage 3 proposal of Object.values/Object.entriesand I'd really like to use it in my current JavaScript project.

我正在查看Object.values/Object.entries第 3 阶段提案,我真的很想在我当前的 JavaScript 项目中使用它。

However, I can't figure out whether there's any Babel preset which supports it. Since the GitHub repository linked above says it's a stage 3 proposal, I assumed it would be part of babel-preset-stage-3, but it seems not.

但是,我不知道是否有任何支持它的 Babel 预设。由于上面链接的 GitHub 存储库说这是第 3 阶段的提案,我认为它是 babel-preset-stage-3 的一部分,但似乎不是。

Is there any Babel preset (or even plugin?) that lets me use Object.entries today?

是否有任何 Babel 预设(甚至插件?)可以让我今天使用 Object.entries?

回答by petersv

Using babel, installing

使用babel,安装

  1. babel-preset-env
  2. babel-plugin-transform-runtime
  1. babel-preset-env
  2. babel-plugin-transform-runtime

gives support for Object.values/Object.entriesas well as other *ES20** functionality.

支持Object.values/Object.entries以及其他 *ES20** 功能。

As per recommendation by the modules, configure .babelrcwith the following:

根据模块的建议,使用以下内容配置.babelrc

{
  "plugins": ["transform-runtime"],
  "presets": ["env"]
}

回答by damd

What I did was install core-jsand then just call this at the top of my file:

我所做的是安装core-js,然后在我的文件顶部调用它:

require('core-js/fn/object/entries');

This made Object.entriesavailable. Credits to @FelixKling.

这使得Object.entries可用。归功于@FelixKling。

Update

更新

In core-js@3, the import path has changed:

在 core-js@3 中,导入路径发生了变化:

require('core-js/features/object/entries');

回答by Randy

Update:

更新

As noted in the comments, you can improve performance by using the mapfunction instead of reduce.

如评论中所述,您可以通过使用map函数而不是来提高性能reduce

Please note that the code is case-sensitive (object != Object).

请注意,代码区分大小写 ( object != Object)。

// Object.values

var objectToValuesPolyfill = function(object) {
    return Object
             .keys(object)
             .map(
                  function(key) {
                    return object[key];
                  }
              );
}

Object.values = Object.values || objectToValuesPolyfill;

// Object.entries

var objectToEntriesPolyfill = function(object) {
    return Object
             .keys(object)
             .map(
                  function(key) {
                    return [key, object[key]];
                  }
              );
}

Object.entries = Object.entries || objectToEntriesPolyfill;

Usage:

用法:

// es6

Object.values = Object.values || (x => Object.keys(x).map(k => x[k]));
Object.entries = Object.entries || (x => Object.keys(x).map(k => [k, x[k]]));

// es5

Object.values = Object.values || function(x){ 
    return Object.keys(x).map(function(k){ 
        return x[k];
    })
};

Object.entries = Object.values || function(x){ 
    return Object.keys(x).map(function(k){ 
        return [k, x[k]];
    })
};


const a = {
  key: "value",
  bool: true,
  num: 123
}

console.log(
  Object.values(a)
)

console.log(
  Object.entries(a)
)