0

Currently , in mycode console.log(JSON.stringify(response, null, 4)) is displaying the result as

[
   {  title: "title of the page",
      Link: "http: www.google.com",
      snippet:"this is google page"
    },
   { title: "title of the second page",
      Link: "http: www.yahoo.com",
      snippet:"this is yahoo page"
    },
    { title: "title of the third page",
      Link: "http: www.drive.com",
      snippet:"this is drive page"
    }
]

Now i need to remove the "[" and "{" "]" "}" in json response and also trim the "title,link,snippet".

The exact result i need is

   "title of the page",
   "http: www.google.com",
  "this is google page"

   "title of second page",
  "http: www.yahoo.com",
  "this is yahoo page"

Please suggest me , how to manipulate this JSON.stringify(response, null, 4)) to get the above result.

  • You can find a solution to your question [here](https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties) – MahanTp Jul 09 '20 at 08:24

2 Answers2

0

Try something like this:

response = [
   {  title: "title of the page",
      Link: "http: www.google.com",
      snippet:"this is google page"
    },
   { title: "title of the second page",
      Link: "http: www.yahoo.com",
      snippet:"this is yahoo page"
    },
    { title: "title of the third page",
      Link: "http: www.drive.com",
      snippet:"this is drive page"
    }
]
response.forEach(x => Object.values(x).forEach(console.log))
orestisf
  • 1,396
  • 1
  • 15
  • 30
0

My take would be like, loop through each index of the array, then get the object values:

let data = [
   {  title: "title of the page",
      Link: "http: www.google.com",
      snippet:"this is google page"
    },
   { title: "title of the second page",
      Link: "http: www.yahoo.com",
      snippet:"this is yahoo page"
    },
    { title: "title of the third page",
      Link: "http: www.drive.com",
      snippet:"this is drive page"
    }
]
let str = ""

data.forEach( obj =>{ Object.values(obj).forEach( (val, key) =>{  str+='"'+val+'"'; key!=2?str+=',\n':str+='\n\n' })} )

console.log(str)
Karl L
  • 1,645
  • 1
  • 7
  • 11