-3

I want to remove single quotes and put double quotes in the array of objects in javascript.

var arr=[{ email: 'abc@gmail.com', name: 'Abc' },
         { email: 'def@hotmail.com', name: 'Dr.Def' },
         { email: 'xyz@gmail.com', name: 'Xyz' },
         { email: 'cad@hotmail.com', name: 'Cad' }];

I want output be like:

var arr=[{"email":"abc@gmail.com","name":"Abc"},
         {"email":"def@hotmail.com","name":"Dr.Def"},
         {"email":"xyz@gmail.com","name":"Xyz"},
         {"email":"cad@hotmail.com","name":"Cad"}];
Khyati Sharma
  • 109
  • 1
  • 9
  • Double quote is the standard when you use `JSON.stringify`. If you're not happy with it, just use `replace`. – sjahan Feb 06 '19 at 12:53
  • `var teachers = JSON.stringify(arr)` – chrisg86 Feb 06 '19 at 12:54
  • Youve just asked here and the answer is correct https://stackoverflow.com/questions/54552242/how-to-remove-single-quote-from-array-of-objects-in-javascript – David Feb 06 '19 at 12:55
  • Wat? What you have there is a Javascript array/object literal. Single and double quotes are exactly equivalent in it. It doesn't matter which one it is. Why do you care? If you want to convert this Javascript array to JSON, in its entirety, then the JSON encoder will use double quotes for strings properly, since that's relevant to JSON. – deceze Feb 06 '19 at 12:57
  • @KhyatiSharma for what purpose you are doing so ? what it does after double-quotes. Try using `JSON.stringify(arr)` `stringify` is made for you . – manikant gautam Feb 06 '19 at 12:58
  • 1
    `JSON.stringify(arr)` returns a JSON **string** not an array. It's unclear what you're asking but you might want to read this [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – adiga Feb 06 '19 at 13:01

1 Answers1

1

Just stringify the whole array?

var arr=[{ email: 'abc@gmail.com', name: 'Abc' },
         { email: 'def@hotmail.com', name: 'Dr.Def' },
         { email: 'xyz@gmail.com', name: 'Xyz' },
         { email: 'cad@hotmail.com', name: 'Cad' }];
         
 console.log(JSON.stringify(arr))

If you want the var arr = too just prepend it on console.log('var arr = ' + JSON.stringify(arr)).

James Coyle
  • 9,922
  • 1
  • 40
  • 48