The The object whose enumerable own property values are to be returned. An array containing the given object's own enumerable property values. Object.values[]
method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in
loop. [The only difference is that a for...in
loop enumerates properties in the prototype chain as well.] Try it
Syntax
Parameters
obj
Return value
Description
Object.values[]
returns an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by looping over the property values of the object manually. Examples
Using Object.values
const obj = { foo: 'bar', baz: 42 };
console.log[Object.values[obj]]; // ['bar', 42]
// Array-like object
const arrayLikeObj1 = { 0: 'a', 1: 'b', 2: 'c' };
console.log[Object.values[arrayLikeObj1 ]]; // ['a', 'b', 'c']
// Array-like object with random key ordering
// When using numeric keys, the values are returned in the keys' numerical order
const arrayLikeObj2 = { 100: 'a', 2: 'b', 7: 'c' };
console.log[Object.values[arrayLikeObj2 ]]; // ['b', 'c', 'a']
// getFoo is property which isn't enumerable
const myObj = Object.create[{}, { getFoo: { value[] { return this.foo; } } }];
myObj.foo = 'bar';
console.log[Object.values[myObj]]; // ['bar']
// non-object argument will be coerced to an object
console.log[Object.values['foo']]; // ['f', 'o', 'o']
Specifications
ECMAScript Language Specification # sec-object.values |
Browser compatibility
BCD tables only load in the browser
See also
I have this object:
var data = {"id": 1, "second": "abcd"};
These are values from a form. I am passing this to a function for verification.
If the above properties exist we can get their values with data["id"]
and data["second"]
, but sometimes, based on other values, the properties can be different.
How can I get values from data
independent of property names?
trincot
282k31 gold badges226 silver badges262 bronze badges
asked Jul 14, 2013 at 1:55
Hari krishnanHari krishnan
1,9503 gold badges17 silver badges25 bronze badges
2
To access the properties of an object without knowing the names of those properties you can use a for ... in
loop:
for[key in data] {
if[data.hasOwnProperty[key]] {
var value = data[key];
//do something with value;
}
}
Superole
1,26924 silver badges28 bronze badges
answered Jul 14, 2013 at 1:58
5
In ES2017 you can use Object.values[]
:
Object.values[data]
At the time of writing support is limited [FireFox and Chrome].All major browsers except IE support this now.
In ES2015 you can use this:
Object.keys[data].map[k => data[k]]
answered Jul 1, 2016 at 21:10
trincottrincot
282k31 gold badges226 silver badges262 bronze badges
If you want to do this in a single line, try:
Object.keys[a].map[function[key]{return a[key]}]
answered Sep 11, 2014 at 21:46
Erel Segal-HaleviErel Segal-Halevi
31.1k32 gold badges107 silver badges165 bronze badges
1
If you $
is defined then You can iterate
var data={"id" : 1, "second" : "abcd"};
$.each[data, function[] {
var key = Object.keys[this][0];
var value = this[key];
//do something with value;
}];
You can access it by following way If you know the values of keys
data.id
or
data["id"]
answered Aug 18, 2015 at 9:53
user3118220user3118220
1,40011 silver badges16 bronze badges
0
I am sorry that your concluding question is not that clear but you are wrong from the very first line. The variable data is an Object not an Array
To access the attributes of an object is pretty easy:
alert[data.second];
But, if this does not completely answer your question, please clarify it and post back.
Thanks !
answered Jul 14, 2013 at 2:11
cooshalcooshal
7386 silver badges20 bronze badges
1
Using lodash _.values[object]
_.values[{"id": 1, "second": "abcd"}]
[ 1, 'abcd' ]
lodash
includes a whole bunch of other functions
to work with arrays, objects, collections, strings, and more that you wish were built into JavaScript [and actually seem to slowly be making their way into the language].
answered Oct 28, 2016 at 0:16
cs01cs01
4,8971 gold badge27 silver badges28 bronze badges
1