skip to Main Content

With this code i want to achieve output name: Gunnar but I get { name: 'Gunnar' }, how do I remove the '' around Gunnar in the output?

let keyName = "name";
let value = "Gunnar";

let person = {
   [keyName]: value
};

console.log(person)

I’ve guess it have something to do with the "value" but i can’t figure out how to fix it.

2

Answers


  1. You can overwrite console.log so it removes " when printing

    const oldLog = console.log
    const logger = (...args) => oldLog(args.map(arg => typeof arg === 'object' ? JSON.stringify(arg, null, 4).replace(/"/g, '') : arg).join(''))
    console.log = logger
    
    let keyName = "name";
    let value = "Gunnar";
    
    let person = {
       [keyName]: value,
    };
    
    console.log(person)
    Login or Signup to reply.
  2. With this code i want to achieve output name: Gunnar but I get { name: 'Gunnar' }, how do I remove the '' around Gunnar in the output?

    Your code outputs an object using console.log. The console.log function will output it in the standard format for outputting objects in your environment. Nothing in your code is designed to create the output you want.

    If you want the output name: Gunnar, then you need to change your console.log statement so you’re outputting that text, rather than telling it to output an object, for instance:

    console.log(`${keyName}: ${value}`);
    
    let keyName = "name";
    let value = "Gunnar";
    
    // No need for this:
    // let person = {
    //     [keyName]: value
    // };
    
    console.log(`${keyName}: ${value}`);

    If you need person to be your starting point, but you still have access to keyName, then you can do it with dynamic property access, like this:

    console.log(`${keyName}: ${person[keyName]}`);
    
    let keyName = "name";
    let value = "Gunnar";
    
    let person = {
        [keyName]: value
    };
    
    console.log(`${keyName}: ${person[keyName]}`);

    If you don’t want to use keyName or value, then it’s not clear what you do want to do. Maybe you want to output multiple lines for each property:

    for (const key in person) {
        console.log(`${key}: ${person[key]}`);
    }
    
    let keyName = "name";
    let value = "Gunnar";
    
    let keyName2 = "age";
    let value2 = 42;
    
    let person = {
        [keyName]: value,
        [keyName2]: value2,
    };
    
    for (const key in person) {
        console.log(`${key}: ${person[key]}`);
    }

    Or maybe you’re expecting an object with exactly one property (although doing that without knowing what the property name is isn’t generally good practice):

    const firstKey = Object.keys(person)[0];
    if (firstKey) {
        console.log(`${firstKey}: ${person[firstKey]}`);
    }
    
    let keyName = "name";
    let value = "Gunnar";
    
    let person = {
        [keyName]: value
    };
    
    const firstKey = Object.keys(person)[0];
    if (firstKey) {
        console.log(`${firstKey}: ${person[firstKey]}`);
    }

    More to explore:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search