如何在JSON对象(javascript)中获取所有键

时间:2021-12-28 13:38:58
{"document":
  {"people":[
    {"name":["Harry Potter"],"age":["18"],"gender":["Male"]},
    {"name":["hermione granger"],"age":["18"],"gender":["Female"]},
  ]}
}

From this JSON example, I would like to get the keys such as name, age, gender for each people.

从这个JSON示例中,我希望获得每个人的名称、年龄、性别等关键字。

How to do this?

如何做到这一点呢?

4 个解决方案

#1


13  

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

这里的key array将返回你的key ["name", "age", "gender"]

#2


12  

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

var keys = [];
for(var i = 0;i<input.document.people.length;i++)
{
    Object.keys(input.document.people[i]).forEach(function(key){
        if(keys.indexOf(key) == -1)
        {
            keys.push(key);
        }
    });
}
console.log(keys);

#3


8  

I use Object.keys which is built into JavaScript Object, it will return an array of keys from given object MDN Reference

我使用对象。在JavaScript对象中构建的键,它将从给定的对象MDN引用返回一个键数组。

var obj = {name: "Jeeva", age: "22", gender: "Male"}
console.log(Object.keys(obj))

#4


2  

ES6 of the day here;

这里的一天;

const json_getAllKeys = data => (
  data.reduce((keys, obj) => (
    keys.concat(Object.keys(obj).filter(key => (
      keys.indexOf(key) === -1))
    )
  ), [])
)

And yes it can be written in very long one line;

是的,它可以写成很长的一行;

const json_getAllKeys = data => data.reduce((keys, obj) => keys.concat(Object.keys(obj).filter(key => keys.indexOf(key) === -1)), [])

#1


13  

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

这里的key array将返回你的key ["name", "age", "gender"]

#2


12  

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

var keys = [];
for(var i = 0;i<input.document.people.length;i++)
{
    Object.keys(input.document.people[i]).forEach(function(key){
        if(keys.indexOf(key) == -1)
        {
            keys.push(key);
        }
    });
}
console.log(keys);

#3


8  

I use Object.keys which is built into JavaScript Object, it will return an array of keys from given object MDN Reference

我使用对象。在JavaScript对象中构建的键,它将从给定的对象MDN引用返回一个键数组。

var obj = {name: "Jeeva", age: "22", gender: "Male"}
console.log(Object.keys(obj))

#4


2  

ES6 of the day here;

这里的一天;

const json_getAllKeys = data => (
  data.reduce((keys, obj) => (
    keys.concat(Object.keys(obj).filter(key => (
      keys.indexOf(key) === -1))
    )
  ), [])
)

And yes it can be written in very long one line;

是的,它可以写成很长的一行;

const json_getAllKeys = data => data.reduce((keys, obj) => keys.concat(Object.keys(obj).filter(key => keys.indexOf(key) === -1)), [])