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 bothname:jack,
andprofession:student}
is likely wrong too, strings must be enclosed in delimiters – CertainPerformance Commented Nov 25, 2018 at 23:37 -
should
profession:student
beprofession:"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, andVar
is notvar
. 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
5 Answers
Reset to default 3First 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)