0

I'm a newbie to Javascript. I wish to know how to get the key as 'personObj', 'carObj, 'workDetailsObj', 'displayFunction' and 'primitiveValue' and print those values in console? Also, how to print the value of the function displayColors() as 'Red Color' in console?. Here is the code I tried.

const personObj = {
  fName: 'John',
  lName: 'Doe',
  age: 30
};
const carObj = {
  type: 'Fiat',
  model: 500,
  color: 'blue'
};
const workDetailsObj = {
  designation: 'Software Engineer',
  salary: 30000,
  company: 'GE'
};

function displayColors() {
  console.log(`Red Color`);
}
let primitiveValue = `Map Object's Contents`;

const mapObj = new Map();

mapObj.set(personObj, 1);
mapObj.set(carObj, 2);
mapObj.set(workDetailsObj, 3);
// mapObj.set(displayFunction, 4);
mapObj.set(primitiveValue, 5);

for (let key of mapObj.keys()) {
  console.log(key);
}

But I want the output to be printed as

personObj details: 

fName : 'John'
 lName : 'Doe' 
age : 30 

carObj details: 
type: 'Fiat' 
model: 500 
color: 'blue' 

workDetailsObj details: 
designation: 'Software Engineer' 
salary: 30000 
company: 'GE' 

displayFunction output: 
Red Color 

primitiveValue value 

: Map Object's Contents.
8
  • 3
    Objects don't have names. The map doesn't "remember" what the variable name was that was used to insert elements into it. If you want to include the variable name in the map, you'll have to put it in the objects. As a side note, it's very unusual to make a map that has objects as keys and numbers as values, and it's difficult to think of any practical use for it. Usually it would be the other way around. Commented Dec 4, 2024 at 16:47
  • Thanks. Ya, ofcourse - it's very unusual to make a map that has objects as keys and numbers as values. Just learning the concepts and thought of trying this code. I tried this way, let txt=''; mapObj.forEach((value, key) => { if(key === personObj){ for(let i=0; i<Object.keys(personObj).length; i++){ txt += Object.keys(personObj)[i] + ': ' + bject.values(personObj)[i] + '\n'; } } console.log(txt). Here, the 'key' is compared to 'personObj'. If so, why it's not possible to print the value as 'personObj' which is stored in 'key'? Thanks. Commented Dec 4, 2024 at 17:01
  • Your keys are not strings, so you would need to convert them to strings. Commented Dec 4, 2024 at 17:02
  • 1
    print the value as 'personObj' which is stored in 'key' – this is not correct. The object is stored in key. Again, variable names are not stored anywhere. Commented Dec 4, 2024 at 17:07
  • 3
    No. This is the third time I'll say this: Objects don't have names. 'personObj' is not the name of any object. Objects don't have names. 'personObj' is not stored anywhere. Commented Dec 4, 2024 at 17:16

1 Answer 1

0

In JavaScript, objects of any type (except functions, and perhaps symbols) do not "know" their names. Only the code that manages them has their name.

The standard approach to logging an object with its name is manual: enter the object name and the object into console.log.

If you still care about the convenience of logging using only the name, you can try creative solutions such as using the eval() function (which is usually dangerous, but will not hurt for debugging purposes).

function log(objname) {
    const obj = eval(objname);
    console.log(`${objname} details:`);
    for (const [key, prop] of Object.entries(obj)) {
        console.log(`    ${key}: ${prop}`);
    }
}

You can also consider enclosing your objects inside another object, so that the variable name becomes a key inside the new object.

const bigObj = {personObj, carObj};
for (const [key, val] of Object.entries(bigObj)) {
    console.log(`\n${key} details:`);
    for (const [k, v] of Object.entries(val)) {
        console.log(`    ${k}: ${v}`);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Even functions can go by many names. const a = function b(c) { console.log(c); } a(a); - is the function that's being logged named "a", "b" or "c" - or all of them?
Both names will refer to the function, but there are differences. The name a will only be available after the declaration, while b is available from the beginning of the code. There are other minor differences.
Actually no, b is only available within the function itself, but the point is that multiple variables with different names are referring to the same object. So saying that "functions do know "their" name" is not helpful imo - and having a string-valued .name property is something that you can do with an ordinary object as well.
You are right, I was not precise. What I wrote is correct for this case: function b() {...}; const a = b;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.