0

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 call Object.keys(foo)[0] But 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, validation time. So I need to get string field name from field 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 as parameter? Can anyone give me an idea?

Hieu Tran
  • 33
  • 1
  • 6
  • 1
    [https://stackoverflow.com/questions/22565077/javascript-get-object-property-name](https://stackoverflow.com/questions/22565077/javascript-get-object-property-name) – Cid Feb 12 '20 at 09:58
  • you can get the list of them with Object.keys – Evgeny Timoshenko Feb 12 '20 at 09:58
  • 1
    Does this answer your question? [Javascript get Object property Name](https://stackoverflow.com/questions/22565077/javascript-get-object-property-name) – Evgeny Timoshenko Feb 12 '20 at 09:59
  • thank you all for answers, 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. – Hieu Tran Feb 12 '20 at 10:06
  • You mean `let key = foo["firstName"] ? "firstname" : "Not available"` – mplungjan Feb 12 '20 at 10:20
  • Or `let found = Object.keys(foo).includes("firstName")` – mplungjan Feb 12 '20 at 10:21
  • 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. – Hieu Tran Feb 13 '20 at 02:36
  • Your comment or background makes no sense. Sorry – mplungjan Feb 13 '20 at 05:37
  • Please give more examples of input and expected output – mplungjan Feb 13 '20 at 05:41
  • @mplungjan Thank you very much for your support. My example of input is json object and its field : foo and foo.firstName. And expected output is string 'firstName'. Is It possible to do that? – Hieu Tran Feb 13 '20 at 08:06
  • You are asking to find .firstName without hardcoding firstName but using firstName as a parameter to find firstName - Please show the actual JSON and explain what you have available to find the key. It is not clear how to find an actual string without using some kind of comparison – mplungjan Feb 13 '20 at 08:23
  • @mplungjan I'm sorry for my confuse question. I mean i need to get string 'firstName' by foo.firstName, not the hard code index 0 from Object.keys(foo)[0] because if index was changed I could not detect the coding issue. Is It possible to do that? – Hieu Tran Feb 13 '20 at 08:33
  • If you have the word firstName, why do you need to get the string "firstName" from the object? – mplungjan Feb 13 '20 at 08:59
  • 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. – Hieu Tran Feb 13 '20 at 09:30
  • So you have an object and a function `let foo = { firstName: 'David', lastName: 'Divad' }; console.log(getFieldName(foo.firstName));` What could change here and how would you expect to find it? – mplungjan Feb 13 '20 at 09:34
  • 1
    @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 . – Hieu Tran Feb 13 '20 at 09:46
  • That still makes no sense at all. If you already in your hand have the word "firstName" to be able to construct "foo.firstName" then you seem to just want to know if the key exists. – mplungjan Feb 13 '20 at 09:54

2 Answers2

2

You can try with Object.keys()

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

console.log(Object.keys(foo)[0]);
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

You can get field name from this and iterate it

Object.keys(foo);