-1

I want to remove single quotes from 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":"Cat"}'];

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":"Cat"}];
Khyati Sharma
  • 109
  • 1
  • 9
  • Your question doesn't make sense now. The `arr` in both of your snippets will be the same if you run it. See https://jsfiddle.net/8o4z7agL/ It seems as if you are mixing up the quotes in your code and the quotes in strings themselves. – Ivar Feb 06 '19 at 14:39
  • 1
    @Ivar you might be understand issue 1st understand issue then response – Ajay yadav Mar 06 '19 at 06:36
  • @Ajayyadav One of the purposes of comments [is to request clarification](https://stackoverflow.com/help/privileges/comment). I tried to explain why this question does not make sense so OP can understand and if necessary clarify. The question has been edited a day after my comment, but [as it stood](https://stackoverflow.com/revisions/54552242/3) both versions [resulted in the exact same JavaScript object](https://jsfiddle.net/8o4z7agL/). – Ivar Mar 06 '19 at 09:08

4 Answers4

3

You can use Array.prototype.map()

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

and JSON.parse()

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

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":"Cat"}'];

var res = arr.map(info => JSON.parse(info));
// Or simply
// var res = arr.map(JSON.parse);
console.log(res);
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • but JSON.parse(info) parse double quotes also. I don't want to remove my double quotes. – Khyati Sharma Feb 06 '19 at 11:20
  • 1
    @KhyatiSharma The output of this code is the same as when you declare your `arr` the way you did in your second snippet. If this doesn't answer your question please [edit](https://stackoverflow.com/posts/54552242/edit) it and elaborate more on what you are trying to achieve. – Ivar Feb 06 '19 at 11:24
0

JSON.parse the objects individually and store inside a new array and print it.

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":"Cat"}'];
  var newarr=[];
arr.forEach((e)=>newarr.push(JSON.parse(e)))
console.log(newarr)
  
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

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":"Cat"}'];
      let output = [];
      for (let i = 0; i < arr.length; i++) {
        output.push(JSON.parse(arr[i]));
      }
      console.log(output);
-1
// Try this code by the "for in loop" method.

let 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":"Cat"}' ];
    
let newArr = [];
for (const item in arr)
{
    newArr.push(JSON.parse(arr[item]))
} 
console.log(newArr)
Force Bolt
  • 1,117
  • 9
  • 9