106

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?

2
  • 6
    This is an Object, not an Array. Commented Jul 14, 2013 at 1:57
  • 2
    First, that's not an array. Second, what you're calling the "index" is normally called the property name or key. There's no way to get the value except by the property. Though any expression can be placed inside the [] and its return value will be used as the property name. Commented Jul 14, 2013 at 1:58

6 Answers 6

116

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;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Don't forget to check hasOwnProperty
Leave the .hasOwnProperty check out. Let's not spread the use of unnecessary guards. It's bad practice.
@CrazyTrain let me just go add to Object.prototype on pages where you have code xD
@PaulS.: Sure, you could do that... and I'll go delete Object.prototype.hasOwnProperty on pages where you have code. :D The only real guard against such things is to have a strict requirement for a clean environment.
93

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])

Comments

52

If you want to do this in a single line, try:

Object.keys(a).map(function(key){return a[key]})

1 Comment

Might be useful to point out that Object.keys() was introduced in ECMA5, supported by IE9+.
16

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"]

Comments

5

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 !

1 Comment

the question is without knowing the key names, your justification did not justify the asked question
3

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).

1 Comment

should not use lodash, it will increase the size ob bundle

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.