Jasmin
Jasmin

Reputation: 39

How to remove single quotes from array

I'm trying to output an object array from an existing array I have, however, the existing array doesn't have keys so in an attempt to create it I did this

  ...
  var range = [];
  for (var i = 0; i < dateArray.length; i ++ ) {
      range.push('{ date: "'+dateArray[i]+'" }')
  }
  var fake = "'"+myArray+"'"
  var p = fake.replace(/[']+/g, '')
  var o = [p]

console logging my "o" variable gives me this....

[ '{ date: "Wed Jun 08 2016 12:00:00 GMT-0400 (EDT)" },{ date: "Thu Jun 09 2016 12:00:00 GMT-0400 (EDT)" }...']

The problem is that my objects within the array get's wrapped by single quotes, causing it to be recognized as one big string.

Seeing how this is no longer recognized as a string, I cannot do str.replace to get rid of the unwanted quotes. Ultimately I want it to look like this:

[ { date: "Wed Jun 08 2016 12:00:00 GMT-0400 (EDT)" },{ date: "Thu Jun 09 2016 12:00:00 GMT-0400 (EDT)" }...]

Upvotes: 0

Views: 2680

Answers (2)

Hector Davila
Hector Davila

Reputation: 310

You only have to remove the single quotation mark when add it to the array. You must add to the array an object and not a string.

var range = [];
  for (var i = 0; i < dateArray.length; i ++ ) {
      range.push({ date: dateArray[i] })
  }

Upvotes: 0

The Dembinski
The Dembinski

Reputation: 1519

Have you tried:

  var range = [];
  for (var i = 0; i < dateArray.length; i ++ ) {
      range.push({ date: dateArray[i].toString() })
  }

Upvotes: 1

Related Questions