skip to Main Content
{"content":"Better than a thousand hollow words, is one word that brings peace."}

How to print the quote from above object without removing doubleqots frm "content" using javascript?

How to print the statement in the console?

3

Answers


  1. var obj = {"content":"Better than a thousand hollow words, is one word that brings peace."};
    
    console.log(obj.content);
    
    Login or Signup to reply.
  2. To access an object’s content property, there’s 2 notations in JavaScript:

    1. dot-notation

      obj.content
      
    2. bracket-notation

      obj["content"]
      

    For the bracket notation, the name of the property to be accessed can also be stored in a variable, which often occurs when looping over many objects with possibly unknown properties, and it is mandatory to use it whenever the name of the property is an invalid identifier. This is usually the case when the property name contains problematic characters, e.g. space(s), some non-alphanumeric characters and especially those ones that Javascript will interpret as an operator unless quoted (that is, part of a string).

    Login or Signup to reply.
  3. The double quotes are not printed in the console unless the key is made up of multiple words. Even then, only single quotes is usually printed.

    A workaround would be to do it manually:

    let x = {'content' : 'Hellow1'}
    console.log(`{"content":"${x.content}"}`)

    Additionally, if you would like to extend this feature to all key-value pairs in the object, then you may write the following:

    let x = {
      'key1' : 'value1', 
      'key2' : 'value2',
      'key3' : 'value3'
     }
     
     for (let i in x) {
      console.log(`{"${i}":"${x[i]}"}`)
     }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search