-1
myArray = ["{url: '/delete', key: 71}", "{url: '/delete', key: 72}", "{url: '/delete', key: 73}", "{url: '/delete', key: 74}"]

I want to remove the " character that wrapped the object. Or escape from double quotes.

I have try this

myArray.replace(/"/g,"")

but it says:

Uncaught TypeError: myArray.replace is not a function

Anybody have the same experience? thank you

3
  • 2
    Where are you getting that data from? The format suggests it's supposed to be JSON, but the format is invalid. Commented Sep 29, 2015 at 6:28
  • Removing the quotes won't suddenly make the contents objects. If that's what you're trying to do. Commented Sep 29, 2015 at 6:28
  • I got the data from ruby array Commented Sep 29, 2015 at 6:36

1 Answer 1

2

You can use map() and eval() to convert the string array to object array

The eval() method evaluates JavaScript code represented as a string.

var myArray = ["{url: '/delete', key: 71}", "{url: '/delete', key: 72}", "{url: '/delete', key: 73}", "{url: '/delete', key: 74}"];

myArray = myArray.map(function(v) {
  return eval('(' + v + ')');
});

document.write(JSON.stringify(myArray));

It's not a good approach, you can send data in valid json format. Which will be much safer than the above method. eg : ["{\"url\": \"/delete\", \"key\": 71}", "{\"url\": \"/delete\", \"key\": 72}", "{\"url": \"/delete\", \"key\": 73}", "{\"url\": \"/delete\", \"key\": 74}"], in this case you can use JSON.parse() instead of eval()

In ruby you can convert the array to JSON string , refer Ruby: How can I convert an array of data to hash and to json format? , Parsing a Ruby Array to JSON .

Sign up to request clarification or add additional context in comments.

1 Comment

@kraf : it's not valid JSON

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.