-2

I would transform the name of an object dynamically in string.

I would transform the name of the object in string to retrieve it dynamically. output desired:

"CartModel"

Currently it returns me

"[object Object]"

Here my object:

var CartModel= {
  userID:{ 
   beverage:{ 
    // value
   },
   sandwich:{ 

   },
   treat:{

   },
   dessert:{

   }
  }
  
   }
   
console.log('"' + CartModel + '"'); 

any hint would be great, thanks

Webwoman
  • 10,196
  • 12
  • 43
  • 87
  • 1
    the object does not know its name ... – Nina Scholz Mar 04 '19 at 17:01
  • 1
    You could use ES6 shorthand property syntax to create an object and get the name: `console.log(Object.keys({CartModel})[0])` but you already know that since it's your code. Why do you need this? – adiga Mar 04 '19 at 17:03
  • That name "variable' is hard-code. It won't change. If I get it correctly you want to figure out the name of something hard-coded. Just use the name you typed. – Dalorzo Mar 04 '19 at 17:03
  • there is necessary a mean to retrieve the name of the object in a string way – Webwoman Mar 04 '19 at 17:03
  • 3
    your use case is unclear. WHY do you want to do this? For what purpose. Perhaps there is a way to solve your problem if you tell us what the problem is rather than what you want your solution to be. – Randy Casburn Mar 04 '19 at 17:07
  • Okay I want to verify in my database if there is a specific object, so using Redis currently, Redis needs the object's name, hence I need to stringify the object's name, that's my WHY – Webwoman Mar 04 '19 at 17:08
  • 1
    The name of the variable that refers to the object is not the object's name. – Pointy Mar 04 '19 at 17:09
  • I want the name of the variable that refers to the object. – Webwoman Mar 04 '19 at 17:09
  • @Pointy can you elaborate please? – Webwoman Mar 04 '19 at 17:10
  • 1
    Read this: https://developer.mozilla.org/en-US/docs/Glossary/Identifier. 2nd paragraph, third sentence. – Randy Casburn Mar 04 '19 at 17:11
  • @RandyCasburn hmmm. thanks for this document – Webwoman Mar 04 '19 at 17:11
  • besides, you access redis data structures with redis keys. The key is unlikley to be the javascript identifier. – Randy Casburn Mar 04 '19 at 17:13
  • @RandyCasburn why it couldn't be? a key is just an identifier in some way – Webwoman Mar 04 '19 at 17:14
  • sure, but look at the code that stores your Object structure into redis. A Redis key was either provided by code or auto-generated and returned. THAT key is what you need to perform a look up in redis. – Randy Casburn Mar 04 '19 at 17:16

3 Answers3

1

You're asking for an object to return the name of the variable it's assigned to. This is impossible. An object doesn't know the name of the variable it's assigned to. Some objects can be assigned to multiple variables, and it's also possible that an object isn't assigned to any variable.

You can customise the value an object displays when it is converted to a String by overriding toString(). For example

CartModel.toString = function () {
  return 'CartModel';
}

Now if you call

console.log('"' + CartModel + '"'); 

"CartModel" will be shown in the console:

Dónal
  • 185,044
  • 174
  • 569
  • 824
0

If you want to print whole object in string then you can use JSON.stringify(obj) method

var obj = {
  name:"abc",
  value:{
    a:1,
    b:2
  }
}
console.log(JSON.stringify(obj));

EDIT: In case of printing name of the object you can create a class (function) of the object using new keyword and print the name using constructor

function test(x, y){
  this.x = x;
  this.y = y;
}

var abc = new test(1,2);
console.log(abc.constructor.name);
Maulik
  • 11
  • 4
0

If it were a function instead of an object you could retrieve it's name.

Or you could provide a toString method ie. var User = { toString: () => 'some name' };

Then if you perform an action that calls the objects toString method it would work.

AKnox
  • 2,455
  • 3
  • 19
  • 19