Javascript jQuery 中的 Object.values()

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

Object.values() in jQuery

javascriptjqueryprototypejs

提问by indieman

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

prototypeJS 库有一个方法 Object.values() ,它返回一个对象中的值数组。

EG:

例如:

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

is there a jQuery method that does the same thing?

是否有一个 jQuery 方法可以做同样的事情?

采纳答案by Randy

Using ES6, you can do the following:

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

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

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

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



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

注意:Object.values()目前是 ES7 的草案

Using babel, installing

使用 babel,安装

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

gives support for Object.values/Object.entriesas well as other ES2017functionality.

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

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

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

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

回答by nnnnnn

I don't think there's a method that does it directly, but you can use $.map():

我认为没有直接执行此操作的方法,但您可以使用$.map()

$.map(myObj, function(val, key) { return val; }); //returns ["val1", "val2"]

(Note though that if the callback returns nullor undefinedfor a given property that item will not be included in the new array, so if your object might have properties with those values you'll have to do it another way. It's pretty easy to code from scratch though, with a for..inloop.)

(但请注意,如果回调返回nullundefined对于给定的属性,该项目将不会包含在新数组中,因此,如果您的对象可能具有具有这些值的属性,您将不得不以另一种方式进行。编码非常容易虽然划伤,但有一个for..in循环。)

回答by osahyoun

prototypejs' valuesmethod extends JavaScript's builtin Objectobject. There's nothing stopping you from doing the same:

prototypejs 的values方法扩展了 JavaScript 的内置Object对象。没有什么能阻止你做同样的事情:

Object.values = function(object) {
  var values = [];
  for(var property in object) {
    values.push(object[property]);
  }
  return values;
}


var foo = {a:1, b:2, c:3};
console.log(Object.values(foo));
// [1, 2, 3]

Alternatively, you can add the method described above to the jQuery object if you'd rather not tamper with Object:

或者,如果您不想篡改,可以将上述方法添加到 jQuery 对象Object

$.values = function() { ... }

回答by mhakes

Underscorejshas the _.valuesmethod: _.values({one : 1, two : 2, three : 3}); => [1, 2, 3]

Underscorejs_.values方法: _.values({one : 1, two : 2, three : 3}); => [1, 2, 3]

This library augments JQuery very well - as well as working wonderfully in prototypejs.

这个库很好地增强了 JQuery - 并且在prototypejs 中也能很好地工作。