jQuery中的Object.values() [英] Object.values() in jQuery

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

问题描述

prototypeJS库具有Object.values()方法,该方法返回对象中的值数组.

The prototypeJS library has a method Object.values() which returns an array of values in an object.

EG:

 var myObj = {
   "key1" : "val1"
   "key2" : "val2"
 }
 Object.values(myObj) //returns ["val1", "val2"]

有没有做相同事情的jQuery方法?

is there a jQuery method that does the same thing?

推荐答案

使用ES6,您可以执行以下操作:

Using ES6, you can do the following:

Object.values = x =>
        Object.keys(x).reduce((y, z) =>
            y.push(x[z]) && y, []);

这只是返回一个包含对象值的数组.不需要JQuery,_或其他任何东西.

This simply returns an array containing the object's values. No need for JQuery, _ or anything else.

注释: Object.values()当前正处于ES7的草案中

note: Object.values() is currently in draft for ES7

使用babel进行安装

Using babel, installing

  • babel-preset-es2017
  • babel-plugin-transform-runtime

提供对Object.values/Object.entries以及其他ES2017功能的支持.

gives support for Object.values/Object.entries as well as other ES2017 functionality.

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

As per recommendation by the modules, configure the .babelrc file with the following:

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

这篇关于jQuery中的Object.values()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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