admin管理员组文章数量:1230185
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
|
Show 1 more comment
3 Answers
Reset to default 10You 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.
本文标签: Get name of an Array or Object by JavascriptStack Overflow
版权声明:本文标题:Get name of an Array or Object by Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739413894a2162196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
console.log(someFunction(thisObject))
then it return"thisObject"
– trungh13 Commented Jun 23, 2018 at 21:42console.log(someFunction(thisObject))
, why can't you write:console.log("thisObject")
– Mark Commented Jun 23, 2018 at 21:48