Answer the question
In order to leave comments, you need to log in
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
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question