W
W
weslyg2016-09-13 11:02:11
JavaScript
weslyg, 2016-09-13 11:02:11

How to get the Name of an Object in JavaScript?

There is an object
var date = {
"man1" : "1974, 10, 29"
}
How to get its name? In this case date.
I just pass an object from an external file to a function, there can be a lot of objects (in different places passed to this function) and I need to return an object from the function that would be in the _date format so that later I can easily look for a specific processed object and not get confused .
I can not find the answer in any way by finding the name of the object. Putting a name on an object is not an option.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kovalsky, 2016-09-13
@weslyg

Well, the names of variables given through var cannot be obtained. It seems to me that in your case the only valid option is to use your objects (for example, date ) inside other objects. Then the keys can be collected using Object.keys , or just for (key in obj)
UPD: I most likely misunderstood what you need, but still tried:

obj = {
  date: {
    "man1" : "1974, 10, 29"
  }
}

result_obj = {};

Object.keys(obj).forEach(function(key) {
    result_obj['_' + key] = obj[key];
});

console.log(result_obj);

And why do you need to pass an object with a changed name? In most cases, when I had to reach my left foot to my right ear, in the end it turned out that the task was solved much easier xD

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question