102

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
  • 317,000
  • 35
  • 244
  • 286
Hari krishnan
  • 2,060
  • 4
  • 18
  • 27
  • 6
    This is an _Object_, not an _Array_. – Paul S. Jul 14 '13 at 01: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. –  Jul 14 '13 at 01:58

6 Answers6

115

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,329
  • 21
  • 29
cfs
  • 10,610
  • 3
  • 30
  • 43
  • 3
    Don't forget to check `hasOwnProperty` – Paul S. Jul 14 '13 at 01:58
  • 4
    Leave the `.hasOwnProperty` check out. Let's not spread the use of unnecessary guards. It's bad practice. –  Jul 14 '13 at 01:59
  • 2
    @CrazyTrain let me just go add to `Object.prototype` on pages where you have code xD – Paul S. Jul 14 '13 at 02:09
  • 6
    @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. –  Jul 14 '13 at 02:31
91

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])
trincot
  • 317,000
  • 35
  • 244
  • 286
51

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

Object.keys(a).map(function(key){return a[key]})
Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
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"]
user3118220
  • 1,438
  • 12
  • 16
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 !

cooshal
  • 758
  • 8
  • 21
  • 2
    the question is without knowing the key names, your justification did not justify the asked question – Irrfan23 Nov 23 '19 at 12:44
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).

cs01
  • 5,287
  • 1
  • 29
  • 28