-1

I have an array like this:

["[Date.UTC(2016,09,30),250500.00]","[Date.UTC(2016,09,29),255100.83]", "[Date.UTC(2016,09,28),255600.82]"]

How can I convert that into something like this?

[[Date.UTC(2016,09,27),256100.83],[Date.UTC(2016,09,25),261091.57],[Date.UTC(2016,09,23),261391.58]]

I am trying this in AngularJS.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
sansid1983
  • 239
  • 1
  • 4
  • 14

5 Answers5

1

There you go.

It's not pretty, but it works, and it doesn't use eval.

var a = ["[Date.UTC(2016,09,30),250500.00]","[Date.UTC(2016,09,29),255100.83]", "[Date.UTC(2016,09,28),255600.82]"];
var result = a.map(function(element){
    var row = JSON.parse(element.replace('Date.UTC(', '').replace(')', '').replace(/,0/g, ','));
    return [new Date(row[0], row[1], row[2]), row[3]];
});
console.log(result);

This returns the array, with the dates as JS Date objects.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

try this

const inputs = [
  "[Date.UTC(2016,09,30),250500.00]",
  "[Date.UTC(2016,09,29),255100.83]", 
  "[Date.UTC(2016,09,28),255600.82]"
]

const result = inputs.map(item => {
  const trimItem = item
    .replace('[Date.UTC(', '')
    .replace(']', '')
    .replace(')', '')
  
  const trimItems = trimItem.split(',')
  
  const YYYY = trimItems[0]
  const MM = trimItems[1]
  const DD = trimItems[2]
  const result = [
     Date.UTC(YYYY, MM, DD),
     Number(trimItems[3])
  ]
  
  return result
})

console.log(result)
Alongkorn
  • 3,968
  • 1
  • 24
  • 41
0

Here is a similar solution using some regex :) I wonder if month starts with zero or one as index! Javascript date takes month with zero index.

console.log(["[Date.UTC(2016,09,30),250500.00]","[Date.UTC(2016,09,29),255100.83]", "[Date.UTC(2016,09,28),255600.82]"].map(function(i){
  var afloat;
  var date = /Date\.UTC\((\d{4}),(\d{2}),(\d{2})\)/.exec(i);
  date = new Date(Date.UTC(+date[1], +date[2] - 1, +date[3]));
  afloat = +i.split('),')[1].split(']')[0];
  return [date, afloat];
}))
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0
data = ["[Date.UTC(2016,09,30),250500.00]","[Date.UTC(2016,09,29),255100.83]", "[Date.UTC(2016,09,28),255600.82]"]

var parseItem = function(it) {
    var utcDateReplaced = it.replace(/Date\.UTC\((.*)\)/, function() {
      argsRaw = arguments[1];
      args = argsRaw.split(',');
      return Date.UTC.apply(Date, args);
    })

    return JSON.parse(utcDateReplaced);
}

var result = data.map(parseItem);
console.log(result); 
// ===> [[1477785600000, 250500], [1477699200000, 255100.83], [1477612800000, 255600.82]]

http://jsbin.com/kohexuh/1/edit?js,console

yevt
  • 732
  • 1
  • 6
  • 21
-1

Between georg's eval answer and Cerbrus's replace answer, there is a middle ground that works like eval but does not use eval: new Function

var from = ["[Date.UTC(2016,09,30),250500.00]","[Date.UTC(2016,09,29),255100.83]", "[Date.UTC(2016,09,28),255600.82]"];
var to = from.map( e => new Function( 'return'+e )() ) // To array
             .map( ([a,b]) => [new Date(a), b] ); // date to Date

console.log( to );

This is not safe, obviously, but it is short and does not have the other penalties that often comes with eval.

Community
  • 1
  • 1
Sheepy
  • 17,324
  • 4
  • 45
  • 69
  • 1
    Nice way to hide yourself from eval cops, but still `new Function` is just eval is disguise. – georg Nov 23 '16 at 09:06
  • @georg new Function does evaluate any string you provide, but it doesn't leak scopes like eval, and thus will not pollute them. Scope access is the main reason eval make the whole script slow, because eval affects other code too, not only itself. – Sheepy Nov 23 '16 at 11:43