-1

I have the following object:

var input = {
        "document": {
            "people":[
                {"name":"Harry Potter","age":"18","gender":"Male"},
                {"name":"hermione granger","age":"18","gender":"Female"}
            ]
        }
    }

I do like this :

_.each(result.document[people], function(item){
    console.log(item); 
    //What should I do here ? or I come wrong way ?
});

And in item I get :

{name : 'Harry Potter', age : '18':, gender:'Male'}
{name : 'hermione grange', age : '18':, gender:'Female'}

I would like to get [name,age,gender]. What should I do?

royhowie
  • 11,075
  • 14
  • 50
  • 67
Hikaru Shindo
  • 2,611
  • 8
  • 34
  • 59
  • 1
    Iterate through `input.document.people`. – Ram Nov 14 '14 at 04:37
  • are you looking for values associated to keys name, age and gender? In `_each` try like this, you can get it `console.log([item.name, item.age,item.gender])` – Koti Panga Nov 14 '14 at 04:37
  • 3
    `Object.keys(input.document.people[0])` ? – Felix Kling Nov 14 '14 at 04:38
  • I want to do dynamically. So I don't want to use '.' to chain inside. – Hikaru Shindo Nov 14 '14 at 04:42
  • In the _.each function I put var key = _.keys(item); And I got [[name,age,gender], [name,age,gender], [name,age,gender], ] What should I do more to get only one array ? – Hikaru Shindo Nov 14 '14 at 04:56
  • 2
    You already asked the exact same question. http://stackoverflow.com/questions/26901971/how-to-get-all-key-in-json-object-javascript Did the other answer not help you? – royhowie Nov 14 '14 at 04:57
  • And asked the same question here as well! http://stackoverflow.com/questions/26908285/how-to-get-a-value-from-specificed-key/26908915#26908915 – Gruff Bunny Nov 14 '14 at 09:34
  • You really need to understand the difference between `document.people` and `document[people]` and `document['people']`. A real basic JS tutorial should help you out here. –  Nov 15 '14 at 06:35

5 Answers5

1

If you think your values are dynamic use a function first

var input = {
    "document": {
        "people":[
            {"name":"Harry Potter","age":"18","gender":"Male"},
            {"name":"hermione granger","age":"18","gender":"Female"}
        ]
    }
}

var func = function (one, two) {
  var array = input[one][two];
  var arr =[];
  for (var i=0; i<array.length; i++){
     arr = Object.keys(array[0]);
  }
 return arr;       
}
func("document", "people"); // will return ["name", "age", "gender"]
James Daly
  • 1,357
  • 16
  • 26
1

Try this

var s = {name: "raul", age: "22", gender: "Male"}
   var keys = [];
   for(var k in s) keys.push(k);

Here keys array will return your keys ["name", "age", "gender"]

Rahul G Nair
  • 1,366
  • 10
  • 11
0

Something like this?

_.each(result.document[people], function(item) {
  _.each(item, function(item, key) {
    console.log(key);
 });
});

_.each sends in a second key parameter to the callback function in the case of objects.

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
0

Here you go.. Final answer. (edited to return keys instead of values as per comment)

_.each(result.document[people], function(item){
    //get keys as numerical array
    var num_arr = [];
    for (var key in item) {
        num_arr.push( key );
    }
    console.log(num_arr); // should return ['name', 'age', 'gender']

});
biko
  • 510
  • 6
  • 15
0

OK, Now I know you actually want the name of object, not the value. So I add another code for you. I'm sorry I don't have time to explain now, but this code I wrote does the trick you need.

This shows the name of object:

root_obj=input.document.people[0];
tmp=[];
for(val in root_obj )
{
  tmp.push(val);
}
console.log(tmp);

This shows the value of object:

root_obj=input.document.people;

for(obj in root_obj )
{
  tmp=[];
  for(val in root_obj[obj] )
  {
    tmp.push(root_obj[obj][val]);
  }
  console.log(tmp);
}