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 ?

Ayo Dev
  • 53
  • 1
  • 6
  • 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 Nov 25 '18 at 23:37
  • should `profession:student` be `profession:"student"` ? – Dacre Denny Nov 25 '18 at 23:40
  • i fixed it thanks , correct me if iam not wrong but `"profession":student` is the right syntax – Ayo Dev Nov 25 '18 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 Nov 25 '18 at 23:53
  • You can validate a json online here : https://jsonlint.com – Shim-Sao Nov 26 '18 at 00:39

5 Answers5

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' ]
Boji
  • 118
  • 8
2

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.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Response of a similar question here :

Getting the object's property name

Shim-Sao
  • 2,026
  • 2
  • 18
  • 23
1
Object.keys(table[0])

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

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

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

Azee
  • 1,809
  • 17
  • 23
  • 2
    This is not recommended. Try executing `Object.prototype.foo = "bar"` before running your code for a nice surprise. If you are using `for ... in` to iterate over object keys, almost always you'll want to test `.hasOwnProperty` to rule out intruders. – Amadan Nov 25 '18 at 23:50
  • True. But I assume in this exact situation nothing is being overridden in Object. Iterating in foreach is just faster and works in old IEs. Here are examples of all techniques: https://hackernoon.com/5-techniques-to-iterate-over-javascript-object-entries-and-their-performance-6602dcb708a8 – Azee Nov 26 '18 at 00:17
0
var test = Object.keys(objectname)
console.log(test)
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Nov 02 '22 at 08:02