最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to get json object titles - Stack Overflow

programmeradmin4浏览0评论

I'm a new programmer , I know how to use dictionary/JSON to get the value of whatever you want like for example var x = {"name":jack,"age":20};
then x.name = jack

but what if I've a table that has been imported through an unknown EXCEL file and I just want to know the title of each column , how can i do that ?

for example

Var table = [{"id":0 , "name":jack,"age":25,"profession":student},{"id":1 , "name":nora,"age":22,"profession":student}]

i want to make a javascript function that can inform me with titles of each columns , the number of the columns how can i do that ?

I'm a new programmer , I know how to use dictionary/JSON to get the value of whatever you want like for example var x = {"name":jack,"age":20};
then x.name = jack

but what if I've a table that has been imported through an unknown EXCEL file and I just want to know the title of each column , how can i do that ?

for example

Var table = [{"id":0 , "name":jack,"age":25,"profession":student},{"id":1 , "name":nora,"age":22,"profession":student}]

i want to make a javascript function that can inform me with titles of each columns , the number of the columns how can i do that ?

Share Improve this question edited Nov 25, 2018 at 23:41 Ayo Dev asked Nov 25, 2018 at 23:36 Ayo DevAyo Dev 531 silver badge6 bronze badges 5
  • do note Var table = is invalid syntax, and both name:jack, and profession:student} is likely wrong too, strings must be enclosed in delimiters – CertainPerformance Commented Nov 25, 2018 at 23:37
  • should profession:student be profession:"student" ? – Dacre Denny Commented Nov 25, 2018 at 23:40
  • i fixed it thanks , correct me if iam not wrong but "profession":student is the right syntax – Ayo Dev Commented Nov 25, 2018 at 23:42
  • Unless student is a variable, you are wrong :D Also, note that JavaScript, like most of the popular programming languages, is case-sensitive, and Var is not var. This is correct: var table = [{"id":0 , "name":"jack","age":25,"profession":"student"},{"id":1 , "name":"nora","age":22,"profession":"student"}] – Amadan Commented Nov 25, 2018 at 23:53
  • You can validate a json online here : jsonlint. – Shim-Sao Commented Nov 26, 2018 at 0:39
Add a ment  | 

5 Answers 5

Reset to default 3

First of all, an object in JavaScript doesn't care if it was constructed out of JSON or not. So your object in JavaScript syntax will look like this:

const x = { name: 'jack', age: 20 };

If you now do Object.keys(x), you will get this:

[ 'name', 'age' ]

And Object.keys(table[0]) should be exactly what you want: the column names of your 'table'

[ 'name', 'age' ]

You can get a list of the names of the properties of an object in javascript with this :

Object.keys();

Documentation here :

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

Response of a similar question here :

Getting the object's property name

Object.keys(table[0])

will give you the keys of the first row; presumably the other rows will behave and follow the pattern.

Just iterate over object keys:

for (var key in table[0]) {console.log(key);}

or (will work in most modern browsers):

Object.keys(table[0]).forEach(key => console.log(key));

Don't forget to check if "table" has elements

Also see other possible options

var test = Object.keys(objectname)
console.log(test)
发布评论

评论列表(0)

  1. 暂无评论