5

I have a quite dummy but confusing question. How can we get the name of an exist array or object ?

For example:

thisObject={ first:1, second:2};
thisArray=[1,2,3,4]

I want to get the string "thisObject", "thisArray".

How can we get it ?

Thanks a lot.


Edited:

For more specific. I want to do something like this: console.log(someFunction(thisObject))

then it return

"thisObject"


Edited-2:

const firstArray=[1,2,3] 
const secondArray=["a","b"] 
const render=(arr)=>arr.map(arrr=>console.log(Object.keys({arr})[0]))
render(firstArray)
render(secondArray)

it will return

"arr" "arr" 

Instead of

"firstArray" "secondArray"
Envil
  • 2,687
  • 1
  • 30
  • 42
trungh13
  • 167
  • 1
  • 3
  • 10
  • Possible duplicate of [Variable name as a string in Javascript](https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript) – Victoria Ruiz Jun 23 '18 at 21:30
  • 2
    This really sounds like an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) -- the names of variables should be irrelevant; what is your final goal? – Mark Jun 23 '18 at 21:39
  • for more specific. I want to do something like this: `console.log(someFunction(thisObject))` then it return `"thisObject"` – trungh13 Jun 23 '18 at 21:42
  • @Mark_M : Then what should title i will rename, sir? thank you. – trungh13 Jun 23 '18 at 21:48
  • If you can write `console.log(someFunction(thisObject))`, why can't you write: `console.log("thisObject")` – Mark Jun 23 '18 at 21:48
  • @Mark_M :I'm doing the render in ReactJS and I also want to have the name of array/object also in the render. Yeah of course I can make `someFunction(thisObject,"thisObject")`. But I'm wondering that do we have any way to get the name of array or object. So it will be only `someFunction(thisObject)` . Is it clear enough :D Thanks – trungh13 Jun 23 '18 at 21:52

3 Answers3

10

You can't actually accomplish what you're trying to do in Javascript, but there's a minor little trick that you can use to log an object's name without directly typing it. It's not particularly useful, and won't really work if you're trying to get the original name of a function argument, but you can do:

console.log(Object.keys({thisObject})[0]);
// "thisObject"

As I said, not particularly useful, but I'll be shocked if you can do any better.

ajxs
  • 3,347
  • 2
  • 18
  • 33
  • Thanks a lot. It works :D As I said from beginning, It may be dummy and confusing question but just wanted to ask. – trungh13 Jun 23 '18 at 22:15
  • Glad to help! The only problem is you won't be able to wrap it in a function, otherwise it'll just keep outputting the name of the argument, unfortunately! I hope that still helps. – ajxs Jun 23 '18 at 22:17
  • 1
    ajxs @ajxs :For a bit further. I have the code `const firstArray=[1,2,3] const secondArray=["a","b"] render=(arr)=>arr.map(arrr=>console.log(Object.keys({arrr})[0]))` it will return `arrr arrr` Anyway to show `"firstArray" "secondArray"` – trungh13 Jun 23 '18 at 22:18
  • please check my Edited-2: for more information Thanks – trungh13 Jun 23 '18 at 22:32
  • I mentioned that you'd have this problem in my comment, it'll only give you the name the variable has within the current scope. Not to get too specific, but the Javascript engine has no concept of the variable's original name, only the name the variable has within the current scope, which in this case is `arr`. Anything else is just outrightly impossible. It sounds like you're trying to use this to debug your application, this effort might be better spent learning how to effectively use the debugging tools you already have at your disposal. – ajxs Jun 24 '18 at 00:02
1

You can use window object to access thisObject and thisArray

Like this -

var thisObject={ first:1, second:2};
var thisArray=[1,2,3,4]

console.log(window.hasOwnProperty("thisObject"));
console.log(window.hasOwnProperty("thisArray"));

console.log(window.thisObject);
console.log(window["thisArray"]);
Vivek
  • 1,465
  • 14
  • 29
0

In javascript the variables reference objects, but the objects themselves aren't named. So given an object, you can't really ask it for it's name; it doesn't make sense. When you think about how references are passed around in javascript you realize how problematic what you are attempting is in a general case. For example consider this:

var a = {first_name: "Mark"};
var b = a;
// a and b both point to the same object

If I ask for the name of that object, should it return a, b, or both? What about now:

var a = {first_name: "Mark"};
var b = a;
a = undefined;

This is even more complicated when you consider that the same object can be referenced by names in different scopes and modules. For example:

var a = {first_name: "Mark"};
function test(obj) {
   var t = obj;
   getNames(obj) // <-- what should that return? a? obj? t? all of them?
}

Give a particular scope, you could iterate through the names and find the ones that equal your object, but this is going to be fragile and probably inefficient.

var a = {first_name: "Mark"}
var b = a
var c = {foo: "bar"}

function getNames(obj, scope) {
  for (name in scope){
    try {
      if (scope[name] === obj) console.log(name)
    } catch(e){continue} // avoid localStorage security error
  }
 }
 
 getNames(b, this) // should log 'a' and 'b' because both are in global scope

But again the above code does not really give you the name of the object, it just looks at every name in a particular object (in this case the global object) and and sees which of the object's properties point to the object in question.

Of course not all objects that are in scope even have names. For example::

let arr = [{foo: "bar"}, {foo: "baz"}]
getNames(arr[0]) // <-- what should that return?

If the name of the object is important and you want to display it, you should add a name property to the object and use it instead.

Mark
  • 90,562
  • 7
  • 108
  • 148