如何获取一个Javascript对象的所有属性值(不知道键)?

时间:2022-09-25 08:40:42

If there is an Javascript object:

如果有一个Javascript对象:

var objects={...};

Suppose, it has more than 50 properties, without knowing the property names (that's without knowing the 'keys') how to get each property value in a loop?

假设,它有超过50个属性,不知道属性名(不知道“键”)如何让每个属性值处于循环中?

18 个解决方案

#1


373  

By using a simple for..in loop:

使用简单的for..在循环:

for(var key in objects) {
    var value = objects[key];
}

#2


885  

Depending on which browsers you have to support, this can be done in a number of ways. The overwhelming majority of browsers in the wild support ECMAScript 5 (ES5), but be warned that many of the examples below use Object.keys, which is not available in IE < 9. See the compatibility table.

根据需要支持的浏览器,可以通过多种方式实现。绝大多数的浏览器都支持ECMAScript 5 (ES5),但是要注意的是下面的许多例子都使用了对象。键,在IE < 9中是不可用的。看到桌子的兼容性。

ECMAScript 3+

If you have to support older versions of IE, then this is the option for you:

如果你必须支持旧版本的IE,那么这就是你的选择:

for (var key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
        var val = obj[key];
        // use val
    }
}

The nested if makes sure that you don't enumerate over properties in the prototype chain of the object (which is the behaviour you almost certainly want). You must use

嵌套的if确保您不枚举对象原型链中的属性(这是您几乎肯定想要的行为)。你必须使用

Object.prototype.hasOwnProperty.call(obj, key) // ok

rather than

而不是

obj.hasOwnProperty(key) // bad

because ECMAScript 5+ allows you to create prototypeless objects with Object.create(null), and these objects will not have the hasOwnProperty method. Naughty code might also produce objects which override the hasOwnProperty method.

因为ECMAScript 5+允许您创建带有Object.create(null)的原型对象,而这些对象将没有hasOwnProperty方法。Naughty代码还可以生成覆盖hasOwnProperty方法的对象。

ECMAScript 5+

You can use these methods in any browser that supports ECMAScript 5 and above. These get values from an object and avoid enumerating over the prototype chain. Where obj is your object:

您可以在任何支持ECMAScript 5和以上的浏览器中使用这些方法。它们从对象中获取值,并避免在原型链上枚举。obj是你的对象:

var keys = Object.keys(obj);

for (var i = 0; i < keys.length; i++) {
    var val = obj[keys[i]];
    // use val
}

If you want something a little more compact or you want to be careful with functions in loops, then Array.prototype.forEach is your friend:

如果你想要更紧凑的东西,或者你想要小心,在循环中使用函数,那么Array.prototype。forEach是你的朋友:

Object.keys(obj).forEach(function (key) {
    var val = obj[key];
    // use val
});

The next method builds an array containing the values of an object. This is convenient for looping over.

下一个方法构建包含对象值的数组。这便于循环。

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

// use vals array

If you want to make those using Object.keys safe against null (as for-in is), then you can do Object.keys(obj || {})....

如果你想要使用对象。密钥对空值是安全的(因为输入是),那么您就可以执行对象了。键(obj | | { })....

Object.keys returns enumerable properties. For iterating over simple objects, this is usually sufficient. If you have something with non-enumerable properties that you need to work with, you may use Object.getOwnPropertyNames in place of Object.keys.

对象。键返回枚举属性。对于迭代简单对象,这通常是足够的。如果您有一些您需要使用的不可枚举属性,您可以使用对象。getOwnPropertyNames代替Object.keys。

ECMAScript 2015+ (A.K.A. ES6)

Arrays are easier to iterate with ECMAScript 2015. You can use this to your advantage when working with values one-by–one in a loop:

数组更易于迭代ECMAScript 2015。当你在一个循环中一个一个的工作时,你可以利用这个优势。

for (const key of Object.keys(obj)) {
    const val = obj[key];
    // use val
}

Using ECMAScript 2015 fat-arrow functions, mapping the object to an array of values becomes a one-liner:

使用ECMAScript 2015 fat-arrow函数,将对象映射到一个值数组变成一行:

const vals = Object.keys(obj).map(key => obj[key]);

// use vals array

ECMAScript 2015 introduces Symbol, instances of which may be used as property names. To get the symbols of an object to enumerate over, use Object.getOwnPropertySymbols (this function is why Symbol can't be used to make private properties). The new Reflect API from ECMAScript 2015 provides Reflect.ownKeys, which returns a list of property names (including non-enumerable ones) and symbols.

ECMAScript 2015引入了符号,其中的实例可以用作属性名。要获得一个对象的符号枚举,使用对象。getOwnPropertySymbols(这个函数是符号不能用于私有属性的原因)。来自ECMAScript 2015的新反射API提供了反映。ownKeys,它返回一个属性名列表(包括不可枚举的名称)和符号。

Array comprehensions (do not attempt to use)

Array comprehensions were removed from ECMAScript 6 before publication. Prior to their removal, a solution would have looked like:

在发表之前,从ECMAScript 6中删除了数组理解。在它们被移除之前,一个解决方案应该是:

const vals = [for (key of Object.keys(obj)) obj[key]];

// use vals array

ECMAScript 2017+

ECMAScript 2016 adds features which do not impact this subject. The ECMAScript 2017 specification adds Object.values and Object.entries. Both return arrays (which will be surprising to some given the analogy with Array.entries). Object.values can be used as is or with a for-of loop.

ECMAScript 2016增加了不影响这个主题的特性。ECMAScript 2017规范添加了对象。价值观和Object.entries。两个返回数组(对于某些给定与array. .entries的类比来说,这将是令人惊讶的)。对象。值可以使用为,也可以用for循环来使用。

const values = Object.values(obj);

// use values array or:

for (const val of Object.values(obj)) {
    // use val
}

If you want to use both the key and the value, then Object.entries is for you. It produces an array filled with [key, value] pairs. You can use this as is, or (note also the ECMAScript 2015 destructuring assignment) in a for-of loop:

如果您想要同时使用键和值,那么对象。条目是给你的。它生成一个数组,其中包含[键值]对。您可以使用这个as,或者(请注意2015年的ECMAScript销毁任务):

for (const [key, val] of Object.entries(obj)) {
    // use key and val
}

Object.values shim

Finally, as noted in the comments and by teh_senaus in another answer, it may be worth using one of these as a shim. Don't worry, the following does not change the prototype, it just adds a method to Object (which is much less dangerous). Using fat-arrow functions, this can be done in one line too:

最后,正如评论和teh_senaus在另一个答案中所指出的那样,使用其中一个作为shim可能是值得的。不要担心,下面的内容不会改变原型,它只是将一个方法添加到对象中(这更不危险)。使用fat-arrow函数,也可以在一行中完成:

Object.values = obj => Object.keys(obj).map(key => obj[key]);

which you can now use like

你现在可以用什么?

// ['one', 'two', 'three']
var values = Object.values({ a: 'one', b: 'two', c: 'three' });

If you want to avoid shimming when a native Object.values exists, then you can do:

如果您想避免在本机对象时出现shimming。值存在,那么您可以:

Object.values = Object.values || (obj => Object.keys(obj).map(key => obj[key]));

Finally...

Be aware of the browsers/versions you need to support. The above are correct where the methods or language features are implemented. For example, support for ECMAScript 2015 was switched off by default in V8 until recently, which powered browsers such as Chrome. Features from ECMAScript 2015 should be be avoided until the browsers you intend to support implement the features that you need. If you use babel to compile your code to ECMAScript 5, then you have access to all the features in this answer.

注意您需要支持的浏览器/版本。在实现方法或语言特性的地方是正确的。例如,对ECMAScript 2015的支持在V8之前被默认关闭了,直到最近,Chrome等浏览器都支持了这一功能。2015年ECMAScript的特性应该被避免,直到你打算支持的浏览器实现你需要的功能。如果您使用babel将代码编译为ECMAScript 5,那么您就可以访问这个答案中的所有特性。

#3


27  

Here's a reusable function for getting the values into an array. It takes prototypes into account too.

这里有一个可重用的函数,用于将值输入到数组中。它也需要原型。

Object.values = function (obj) {
    var vals = [];
    for( var key in obj ) {
        if ( obj.hasOwnProperty(key) ) {
            vals.push(obj[key]);
        }
    }
    return vals;
}

#4


25  

If you have access to Underscore.js, you can use the _.values function like this:

如果你有下划线。js,你可以用_。值函数如下:

_.values({one : 1, two : 2, three : 3}); // return [1, 2, 3]

#5


13  

If you really want an array of Values, I find this cleaner than building an array with a for ... in loop.

如果你真的想要一个数组的值,我发现这个比用a构建一个数组更干净。在循环。

ECMA 5.1+

ECMA 5.1 +

function values(o) { return Object.keys(o).map(function(k){return o[k]}) }

It's worth noting that in most cases you don't really need an array of values, it will be faster to do this:

值得注意的是,在大多数情况下,您并不真正需要一系列的值,这样做会更快:

for(var k in o) something(o[k]);

This iterates over the keys of the Object o. In each iteration k is set to a key of o.

这个迭代遍历对象o的键。在每个迭代中,k被设置为o的键。

#6


4  

ES5 Object.keys

ES5种

var a = { a: 1, b: 2, c: 3 };
Object.keys(a).map(function(key){ return a[key] });
// result: [1,2,3]

#7


3  

You can loop through the keys:

你可以通过按键进行循环:

foo = {one:1, two:2, three:3};
for (key in foo){
    console.log("foo["+ key +"]="+ foo[key]);
}

will output:

将输出:

foo[one]=1
foo[two]=2
foo[three]=3

#8


2  

For those early adapting people on the CofeeScript era, here's another equivalent for it.

对于那些早期适应CofeeScript时代的人来说,这里有另一个类似的东西。

val for key,val of objects

Which may be better than this because the objects can be reduced to be typed again and decreased readability.

这可能比这更好,因为对象可以被还原为再次输入并降低可读性。

objects[key] for key of objects

#9


2  

use a polyfill like:

使用一个polyfill:

if(!Object.values){Object.values=obj=>Object.keys(obj).map(key=>obj[key])}

then use

然后使用

Object.values(my_object)

3) profit!

3)利润!

#10


1  

Apparently - as I recently learned - this is the fastest way to do it:

显然,正如我最近学到的,这是最快的方法:

var objs = {...};
var objKeys = Object.keys(obj);
for (var i = 0, objLen = objKeys.length; i < objLen; i++) {
    // do whatever in here
    var obj = objs[objKeys[i]];
}

#11


1  

You can use this object-values component I wrote to get all object values.

您可以使用我编写的对象-值组件来获取所有对象值。

Examples:

例子:

values({ a: 1, b: 2, c: 3 }) // => [1, 2, 3]

Here is how it works:

下面是它的工作原理:

function values(object: {[any]: any}): any[] {
  const objValues = [];
  forEach(object, val => objValues.push(val)); 
  return objValues;
};

This MPM micro package can also do the trick.

这个MPM微包也可以做到这一点。

#12


0  

Here's a function similar to PHP's array_values()

这是一个与PHP的array_values()类似的函数

function array_values(input) {
  var output = [], key = '';
  for ( key in input ) { output[output.length] = input[key]; }
  return output;
}

Here's how to get the object's values if you're using ES6 or higher:

下面是如何在使用ES6或更高版本时获取对象的值:

Array.from(values(obj));

#13


0  

Compatible with ES7 even some browsers do not support it yet

Since , Object.values(<object>) will be built-in in ES7 &

因为object .values()将内置在ES7 &中。

Until waiting all browsers to support it , you could wrap it inside a function :

在等待所有浏览器支持它之前,您可以将其封装到一个函数中:

Object.vals=(o)=>(Object.values)?Object.values(o):Object.keys(o).map((k)=>o[k])

Then :

然后:

Object.vals({lastname:'T',firstname:'A'})
 // ['T','A']

Once browsers become compatible with ES7, you will not have to change anything in your code.

#14


0  

I realize I'm a little late but here's a shim for the new firefox 47 Object.values method

我意识到我有点晚了,但这是新firefox 47的一个shim。值方法

Object.prototype.values = Object.prototype.values || function(obj) {
  return this.keys(obj).map(function(key){
    return obj[key];
  });
};

#15


-1  

var objects={...}; this.getAllvalues = function () {
        var vls = [];
        for (var key in objects) {
            vls.push(objects[key]);
        }
        return vls;
    }

#16


-3  

in ECMAScript5 use

ECMAScript5使用

 keys = Object.keys(object);

Otherwise if you're browser does not support it, use the well-known for..in loop

否则,如果您的浏览器不支持它,请使用众所周知的..在循环

for (key in object) {
    // your code here
}

#17


-8  

Now I use Dojo Toolkit because older browsers do not support Object.values.

现在我使用Dojo Toolkit,因为旧的浏览器不支持Object.values。

require(['dojox/lang/functional/object'], function(Object) {
    var obj = { key1: '1', key2: '2', key3: '3' };
    var values = Object.values(obj);
    console.log(values);
});

Output :

输出:

['1', '2', '3']

#18


-10  

use

使用

console.log(variable)

and if you using google chrome open Console by using Ctrl+Shift+j

如果你使用谷歌chrome打开控制台,使用Ctrl+Shift+j。

Goto >> Console

Goto > >控制台

#1


373  

By using a simple for..in loop:

使用简单的for..在循环:

for(var key in objects) {
    var value = objects[key];
}

#2


885  

Depending on which browsers you have to support, this can be done in a number of ways. The overwhelming majority of browsers in the wild support ECMAScript 5 (ES5), but be warned that many of the examples below use Object.keys, which is not available in IE < 9. See the compatibility table.

根据需要支持的浏览器,可以通过多种方式实现。绝大多数的浏览器都支持ECMAScript 5 (ES5),但是要注意的是下面的许多例子都使用了对象。键,在IE < 9中是不可用的。看到桌子的兼容性。

ECMAScript 3+

If you have to support older versions of IE, then this is the option for you:

如果你必须支持旧版本的IE,那么这就是你的选择:

for (var key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
        var val = obj[key];
        // use val
    }
}

The nested if makes sure that you don't enumerate over properties in the prototype chain of the object (which is the behaviour you almost certainly want). You must use

嵌套的if确保您不枚举对象原型链中的属性(这是您几乎肯定想要的行为)。你必须使用

Object.prototype.hasOwnProperty.call(obj, key) // ok

rather than

而不是

obj.hasOwnProperty(key) // bad

because ECMAScript 5+ allows you to create prototypeless objects with Object.create(null), and these objects will not have the hasOwnProperty method. Naughty code might also produce objects which override the hasOwnProperty method.

因为ECMAScript 5+允许您创建带有Object.create(null)的原型对象,而这些对象将没有hasOwnProperty方法。Naughty代码还可以生成覆盖hasOwnProperty方法的对象。

ECMAScript 5+

You can use these methods in any browser that supports ECMAScript 5 and above. These get values from an object and avoid enumerating over the prototype chain. Where obj is your object:

您可以在任何支持ECMAScript 5和以上的浏览器中使用这些方法。它们从对象中获取值,并避免在原型链上枚举。obj是你的对象:

var keys = Object.keys(obj);

for (var i = 0; i < keys.length; i++) {
    var val = obj[keys[i]];
    // use val
}

If you want something a little more compact or you want to be careful with functions in loops, then Array.prototype.forEach is your friend:

如果你想要更紧凑的东西,或者你想要小心,在循环中使用函数,那么Array.prototype。forEach是你的朋友:

Object.keys(obj).forEach(function (key) {
    var val = obj[key];
    // use val
});

The next method builds an array containing the values of an object. This is convenient for looping over.

下一个方法构建包含对象值的数组。这便于循环。

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

// use vals array

If you want to make those using Object.keys safe against null (as for-in is), then you can do Object.keys(obj || {})....

如果你想要使用对象。密钥对空值是安全的(因为输入是),那么您就可以执行对象了。键(obj | | { })....

Object.keys returns enumerable properties. For iterating over simple objects, this is usually sufficient. If you have something with non-enumerable properties that you need to work with, you may use Object.getOwnPropertyNames in place of Object.keys.

对象。键返回枚举属性。对于迭代简单对象,这通常是足够的。如果您有一些您需要使用的不可枚举属性,您可以使用对象。getOwnPropertyNames代替Object.keys。

ECMAScript 2015+ (A.K.A. ES6)

Arrays are easier to iterate with ECMAScript 2015. You can use this to your advantage when working with values one-by–one in a loop:

数组更易于迭代ECMAScript 2015。当你在一个循环中一个一个的工作时,你可以利用这个优势。

for (const key of Object.keys(obj)) {
    const val = obj[key];
    // use val
}

Using ECMAScript 2015 fat-arrow functions, mapping the object to an array of values becomes a one-liner:

使用ECMAScript 2015 fat-arrow函数,将对象映射到一个值数组变成一行:

const vals = Object.keys(obj).map(key => obj[key]);

// use vals array

ECMAScript 2015 introduces Symbol, instances of which may be used as property names. To get the symbols of an object to enumerate over, use Object.getOwnPropertySymbols (this function is why Symbol can't be used to make private properties). The new Reflect API from ECMAScript 2015 provides Reflect.ownKeys, which returns a list of property names (including non-enumerable ones) and symbols.

ECMAScript 2015引入了符号,其中的实例可以用作属性名。要获得一个对象的符号枚举,使用对象。getOwnPropertySymbols(这个函数是符号不能用于私有属性的原因)。来自ECMAScript 2015的新反射API提供了反映。ownKeys,它返回一个属性名列表(包括不可枚举的名称)和符号。

Array comprehensions (do not attempt to use)

Array comprehensions were removed from ECMAScript 6 before publication. Prior to their removal, a solution would have looked like:

在发表之前,从ECMAScript 6中删除了数组理解。在它们被移除之前,一个解决方案应该是:

const vals = [for (key of Object.keys(obj)) obj[key]];

// use vals array

ECMAScript 2017+

ECMAScript 2016 adds features which do not impact this subject. The ECMAScript 2017 specification adds Object.values and Object.entries. Both return arrays (which will be surprising to some given the analogy with Array.entries). Object.values can be used as is or with a for-of loop.

ECMAScript 2016增加了不影响这个主题的特性。ECMAScript 2017规范添加了对象。价值观和Object.entries。两个返回数组(对于某些给定与array. .entries的类比来说,这将是令人惊讶的)。对象。值可以使用为,也可以用for循环来使用。

const values = Object.values(obj);

// use values array or:

for (const val of Object.values(obj)) {
    // use val
}

If you want to use both the key and the value, then Object.entries is for you. It produces an array filled with [key, value] pairs. You can use this as is, or (note also the ECMAScript 2015 destructuring assignment) in a for-of loop:

如果您想要同时使用键和值,那么对象。条目是给你的。它生成一个数组,其中包含[键值]对。您可以使用这个as,或者(请注意2015年的ECMAScript销毁任务):

for (const [key, val] of Object.entries(obj)) {
    // use key and val
}

Object.values shim

Finally, as noted in the comments and by teh_senaus in another answer, it may be worth using one of these as a shim. Don't worry, the following does not change the prototype, it just adds a method to Object (which is much less dangerous). Using fat-arrow functions, this can be done in one line too:

最后,正如评论和teh_senaus在另一个答案中所指出的那样,使用其中一个作为shim可能是值得的。不要担心,下面的内容不会改变原型,它只是将一个方法添加到对象中(这更不危险)。使用fat-arrow函数,也可以在一行中完成:

Object.values = obj => Object.keys(obj).map(key => obj[key]);

which you can now use like

你现在可以用什么?

// ['one', 'two', 'three']
var values = Object.values({ a: 'one', b: 'two', c: 'three' });

If you want to avoid shimming when a native Object.values exists, then you can do:

如果您想避免在本机对象时出现shimming。值存在,那么您可以:

Object.values = Object.values || (obj => Object.keys(obj).map(key => obj[key]));

Finally...

Be aware of the browsers/versions you need to support. The above are correct where the methods or language features are implemented. For example, support for ECMAScript 2015 was switched off by default in V8 until recently, which powered browsers such as Chrome. Features from ECMAScript 2015 should be be avoided until the browsers you intend to support implement the features that you need. If you use babel to compile your code to ECMAScript 5, then you have access to all the features in this answer.

注意您需要支持的浏览器/版本。在实现方法或语言特性的地方是正确的。例如,对ECMAScript 2015的支持在V8之前被默认关闭了,直到最近,Chrome等浏览器都支持了这一功能。2015年ECMAScript的特性应该被避免,直到你打算支持的浏览器实现你需要的功能。如果您使用babel将代码编译为ECMAScript 5,那么您就可以访问这个答案中的所有特性。

#3


27  

Here's a reusable function for getting the values into an array. It takes prototypes into account too.

这里有一个可重用的函数,用于将值输入到数组中。它也需要原型。

Object.values = function (obj) {
    var vals = [];
    for( var key in obj ) {
        if ( obj.hasOwnProperty(key) ) {
            vals.push(obj[key]);
        }
    }
    return vals;
}

#4


25  

If you have access to Underscore.js, you can use the _.values function like this:

如果你有下划线。js,你可以用_。值函数如下:

_.values({one : 1, two : 2, three : 3}); // return [1, 2, 3]

#5


13  

If you really want an array of Values, I find this cleaner than building an array with a for ... in loop.

如果你真的想要一个数组的值,我发现这个比用a构建一个数组更干净。在循环。

ECMA 5.1+

ECMA 5.1 +

function values(o) { return Object.keys(o).map(function(k){return o[k]}) }

It's worth noting that in most cases you don't really need an array of values, it will be faster to do this:

值得注意的是,在大多数情况下,您并不真正需要一系列的值,这样做会更快:

for(var k in o) something(o[k]);

This iterates over the keys of the Object o. In each iteration k is set to a key of o.

这个迭代遍历对象o的键。在每个迭代中,k被设置为o的键。

#6


4  

ES5 Object.keys

ES5种

var a = { a: 1, b: 2, c: 3 };
Object.keys(a).map(function(key){ return a[key] });
// result: [1,2,3]

#7


3  

You can loop through the keys:

你可以通过按键进行循环:

foo = {one:1, two:2, three:3};
for (key in foo){
    console.log("foo["+ key +"]="+ foo[key]);
}

will output:

将输出:

foo[one]=1
foo[two]=2
foo[three]=3

#8


2  

For those early adapting people on the CofeeScript era, here's another equivalent for it.

对于那些早期适应CofeeScript时代的人来说,这里有另一个类似的东西。

val for key,val of objects

Which may be better than this because the objects can be reduced to be typed again and decreased readability.

这可能比这更好,因为对象可以被还原为再次输入并降低可读性。

objects[key] for key of objects

#9


2  

use a polyfill like:

使用一个polyfill:

if(!Object.values){Object.values=obj=>Object.keys(obj).map(key=>obj[key])}

then use

然后使用

Object.values(my_object)

3) profit!

3)利润!

#10


1  

Apparently - as I recently learned - this is the fastest way to do it:

显然,正如我最近学到的,这是最快的方法:

var objs = {...};
var objKeys = Object.keys(obj);
for (var i = 0, objLen = objKeys.length; i < objLen; i++) {
    // do whatever in here
    var obj = objs[objKeys[i]];
}

#11


1  

You can use this object-values component I wrote to get all object values.

您可以使用我编写的对象-值组件来获取所有对象值。

Examples:

例子:

values({ a: 1, b: 2, c: 3 }) // => [1, 2, 3]

Here is how it works:

下面是它的工作原理:

function values(object: {[any]: any}): any[] {
  const objValues = [];
  forEach(object, val => objValues.push(val)); 
  return objValues;
};

This MPM micro package can also do the trick.

这个MPM微包也可以做到这一点。

#12


0  

Here's a function similar to PHP's array_values()

这是一个与PHP的array_values()类似的函数

function array_values(input) {
  var output = [], key = '';
  for ( key in input ) { output[output.length] = input[key]; }
  return output;
}

Here's how to get the object's values if you're using ES6 or higher:

下面是如何在使用ES6或更高版本时获取对象的值:

Array.from(values(obj));

#13


0  

Compatible with ES7 even some browsers do not support it yet

Since , Object.values(<object>) will be built-in in ES7 &

因为object .values()将内置在ES7 &中。

Until waiting all browsers to support it , you could wrap it inside a function :

在等待所有浏览器支持它之前,您可以将其封装到一个函数中:

Object.vals=(o)=>(Object.values)?Object.values(o):Object.keys(o).map((k)=>o[k])

Then :

然后:

Object.vals({lastname:'T',firstname:'A'})
 // ['T','A']

Once browsers become compatible with ES7, you will not have to change anything in your code.

#14


0  

I realize I'm a little late but here's a shim for the new firefox 47 Object.values method

我意识到我有点晚了,但这是新firefox 47的一个shim。值方法

Object.prototype.values = Object.prototype.values || function(obj) {
  return this.keys(obj).map(function(key){
    return obj[key];
  });
};

#15


-1  

var objects={...}; this.getAllvalues = function () {
        var vls = [];
        for (var key in objects) {
            vls.push(objects[key]);
        }
        return vls;
    }

#16


-3  

in ECMAScript5 use

ECMAScript5使用

 keys = Object.keys(object);

Otherwise if you're browser does not support it, use the well-known for..in loop

否则,如果您的浏览器不支持它,请使用众所周知的..在循环

for (key in object) {
    // your code here
}

#17


-8  

Now I use Dojo Toolkit because older browsers do not support Object.values.

现在我使用Dojo Toolkit,因为旧的浏览器不支持Object.values。

require(['dojox/lang/functional/object'], function(Object) {
    var obj = { key1: '1', key2: '2', key3: '3' };
    var values = Object.values(obj);
    console.log(values);
});

Output :

输出:

['1', '2', '3']

#18


-10  

use

使用

console.log(variable)

and if you using google chrome open Console by using Ctrl+Shift+j

如果你使用谷歌chrome打开控制台,使用Ctrl+Shift+j。

Goto >> Console

Goto > >控制台