1

I have an object, like

let foo = {
  firstName: 'David',
  lastName: 'Divad'
};

I want to get string 'firstName' of foo.firstName. I found there is a way to get it but need to hard code the index: Javascript get Object property Name

I can get 'firstName' by calling Object.keys(foo)[0]. I don't want to hard code the index because if the index was changed, I could not detect error in code at the compile time. With object's field variable we always can detect issue first at the compile time not runtime as other ways.

I need to get string field name from field foo.firstName not by hard code the index or string but object's field variable (ex: foo.firstName).

I wonder if there is a function such as getFieldName below do to that. For example:

let foo = {
  firstName: 'David',
  lastName: 'Divad'
};

console.log(getFieldName(foo.firstName));  // expect print out 'firstName'

or

console.log(getFieldName(foo, foo.firstName));  // expect print out 'firstName'

Is there any way to get field name of a javaScript object with object's field variable as parameter?

20
  • 1
    https://stackoverflow.com/questions/22565077/javascript-get-object-property-name Commented Feb 12, 2020 at 9:58
  • 1
    Does this answer your question? Javascript get Object property Name Commented Feb 12, 2020 at 9:59
  • 1
    @mplungjan : no, i did not mean that. Could you please check my background note: I don't want to hard code 'firstName' or index because if the field name or It's index was changed, I could not detect error in code at the compile, validation time. So I need to get string field name from field foo.firstName. Commented Feb 13, 2020 at 2:36
  • 1
    @mplungjan because string "firsName" will be changed dynamically when i changed the field name foo.firstName. For example I have a sort function which can sort by any fieldname as parameter, I can reuse code. Commented Feb 13, 2020 at 9:30
  • 2
    @mplungjan Thank you for taking your time. I mean I expect a function such as getFieldName which can return string "firsName" from foo.firstName or string 'lastName' from foo.lastName . Commented Feb 13, 2020 at 9:46

2 Answers 2

2

You can try with Object.keys()

let foo = {
  firstName: 'David',
  lastName: 'Divad'
};

console.log(Object.keys(foo)[0]);

Sign up to request clarification or add additional context in comments.

8 Comments

Huge dupe......
thank you for the answer, i mean i need to get string field name with object's field as parameter without any index hard code. Just field as parameter. I have just update the question. Could you give me any idea?
@mplungjan that not dupe, please help me :( I have just update the question
@HieuTran Nothing in your update not making this a dupe. Unless you are not explaining what you actually need
@mplungjan I have update and explain what I need. Could you please help me?
|
0

You can get field name from this and iterate it

Object.keys(foo);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.