简体   繁体   中英

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)" }...]

Have you tried:

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

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] })
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM