-2

I have an object with one key value as given below -

var a = {};
a.x = "randomvalue";

My requirement is to get access the value "randomvalue", but the catch is I dont know that the property name is "x".

Simplest way to get the value??

anand patil
  • 507
  • 1
  • 9
  • 26

1 Answers1

1

Try:

var v, i;
var a = {};
a.x = "randomvalue";
for (x in a) {
  if a[x] === "randomvalue" {
    v = x;
    i = "randomvalue";
  }
}

Then v contains the object key and i contains the object value (although you don't really need it).

Or, if you know the value index:

var obj = { first: 'someVal' };
obj[Object.keys(obj)[0]]; //returns 'someVal'
Abraar Arique Diganto
  • 1,215
  • 16
  • 24