25

Situation

I have a JSON object which is returned. And Below is an example of one. The who in this particular example can change to whatever property name is required. So for example next time this will be name rather than who

 [{"who":"Arthur"},{"who":"Craig"},{"who":"Dan"},{"who":"Daniel"},{"who":"Frank"},{"who":"Ian"},{"who":"jamie"},{"who":"Jason"},{"who":"jaz"},{"who":"Liam"},{"who":"Paul"},{"who":"Shaun"},{"who":"Wayne"}]

Problem

In my JS I need to be able to refer to the property and access its data without using its name as the name will always be something different.

What I have tried

data.forEach(function(m){
    console.info(m); // Object { who="Craig"}
    console.info(m.who); // Craig, as expected
    console.info(m[0]); // now not sure who to get it if who changes to name
});
5
  • 1
    Are you looking for m['who']? Commented May 15, 2013 at 23:12
  • indeed, this would be how I can return who. But I need to be able to return an string. So m['*'] effectively. I do not know the name of the property. Commented May 15, 2013 at 23:13
  • Gotcha, answer added. Commented May 15, 2013 at 23:14
  • does anyone know if this is possible using a jpath query? Commented Nov 9, 2017 at 19:47
  • 1
    Possible duplicate of Accessing elements of JSON object without knowing the key names Commented Dec 5, 2017 at 23:01

3 Answers 3

56

Object.keys(m)[0] should return the first enumerable property name in the object m.

So if m = {"who": "Arthur"}; then m[Object.keys(m)[0]] will be "Arthur".

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys


Alternatively: Object.values(m)[0]. See Object.values

Sign up to request clarification or add additional context in comments.

Comments

6

Use for...in

You can also use the for in loop:

data.forEach( function ( m ) {
  for ( const key in m ) {
    if (m.hasOwnProperty(key)) { // Only print properties that have not been inherited
      console.log( key );        // "who"
      console.log( m[key] );     // "Arthur"
    }
  }

});

The above would also work for multiple key: value pairs in your object i.e:

[ {"who": "Arthur", "who": "Fred"}, {"who": "Craig"}, ]

Issues with the above:

  • Order is not guaranteed when iterating over objects
  • If not using hasOwnProperty you might iterate over inherited properties

Use for...of

Another good option of iterating over objects is with for..of and Object.enteries()

for ( const [key, value] of Object.entries(data) ) {
  console.log( key ); // "who"
  console.log( value ); // "Arthur"  
}
  • Only iterates over enumerable props

Comments

2

If you always expect these objects to have only one property, you could do something like this:

var name, person;
for (person in data) {
    for (name in data[person]) {
        console.log(data[person][name]);
    }
}

This would enumerate through each property of each person in the data. Because there is only one property per person (I assume), it will just enumerate that one property and stop, allowing you to use that property regardless of its name.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.