最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get name of an Array or Object by Javascript - Stack Overflow

programmeradmin0浏览0评论

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"

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"
Share Improve this question edited Dec 14, 2020 at 9:59 Envil 2,7271 gold badge34 silver badges45 bronze badges asked Jun 23, 2018 at 21:27 trungh13trungh13 1671 gold badge3 silver badges10 bronze badges 6
  • Possible duplicate of Variable name as a string in Javascript – Victoria Ruiz Commented Jun 23, 2018 at 21:30
  • 2 This really sounds like an xy problem -- the names of variables should be irrelevant; what is your final goal? – Mark Commented Jun 23, 2018 at 21:39
  • for more specific. I want to do something like this: console.log(someFunction(thisObject)) then it return "thisObject" – trungh13 Commented Jun 23, 2018 at 21:42
  • @Mark_M : Then what should title i will rename, sir? thank you. – trungh13 Commented Jun 23, 2018 at 21:48
  • If you can write console.log(someFunction(thisObject)), why can't you write: console.log("thisObject") – Mark Commented Jun 23, 2018 at 21:48
 |  Show 1 more comment

3 Answers 3

Reset to default 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.

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"]);

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.

发布评论

评论列表(0)

  1. 暂无评论