2

i am a beginner in javascript and i have a litle problem.

I have this :

[
  {
    "date": "2014-04-23 00:00:00",
    "volumetrie": "22458"
  },
  {
    "date": "2014-05-02 00:00:00",
    "volumetrie": "30585"
  },
  {
    "date": "2014-03-27 00:00:00",
    "volumetrie": "49536"
  }
]

And i would like this :

[
  {
    "x": "2014-04-23T22:00:00.000Z",
    "y": 22458
  },
  {
    "x": "2014-05-02T22:00:00.000Z",
    "y": 30585
  },
  {
    "x": "2014-03-27T22:00:00.000Z",
    "y": 49536
  }
]

Do you have any ideas ? Thank you very much for you future answer ;)

Machavity
  • 30,841
  • 27
  • 92
  • 100
alexandre bru
  • 103
  • 2
  • 9
  • 1
    Aren't dates in the resulting array required to be ISO strings ? – xbug Nov 25 '14 at 17:16
  • 1
    Although the question is closed now as a duplicate if you are looking for an exact answer then use new Date().toISOString() to convert the date into your required format. – Alok Swain Nov 25 '14 at 17:21

3 Answers3

8

Use Array.map:

var arr = [
  {
    "date": "2014-04-23 00:00:00",
    "volumetrie": "22458"
  },
  {
    "date": "2014-05-02 00:00:00",
    "volumetrie": "30585"
  },
  {
    "date": "2014-03-27 00:00:00",
    "volumetrie": "49536"
  }
];

var newArr = arr.map(function(item){
   return {x: item.date, y: item.volumetrie};
});
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • Keep an eye on the browser compatibility for this one - IE8 is apparently not supported - you'd have to write your own map or use something like [jQuery](http://api.jquery.com/jQuery.map/) if you need to support it. – Joe Enos Nov 25 '14 at 17:06
4

You can do it by

var origArr = [{"date": "2014-04-23 00:00:00","volumetrie": "22458"},{"date": "2014-05-02 00:00:00","volumetrie": "30585"},{"date": "2014-03-27 00:00:00","volumetrie": "49536"}];

forEach to loop through the elements of the array and create your own

var modifArr = [];

origArr.forEach(function(elem){
    modifArr.push({ x : (new Date(elem.date)).toISOString(), y : elem.volumetrie })
});

console.log(modifArr); // the array that you need

Or map

var modifArr = origArr.map(function(elem){
    return { x : (new Date(elem.date)).toISOString(), y : elem.volumetrie }
});

console.log( modifArr );

or jQuery's each

var modifArr = [];

$(origArr).each(function(index, elem){
    modifArr.push({ x : (new Date(elem.date)).toISOString(), y : elem.volumetrie })
});

console.log( modifArr );
Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101
  • Please don't use jQuery for things like this. – pablochan Nov 25 '14 at 17:03
  • 1
    @pablochan It looks like `forEach` is not available in IE8, so jQuery would be a reasonable alternative if you need to support that. Still the biggest reason to use jQuery for me is to handle old browsers - a reasonable percentage of users are still on IE8. – Joe Enos Nov 25 '14 at 17:08
  • @JoeEnos The snarky answer would be "Please don't support IE8", but I won't say that. I will concede that if you already have jQuery for other things, using it is not that bad. – pablochan Nov 25 '14 at 17:12
  • @pablochan Yep, I try to tell my boss to not support IE8 all the time. Doesn't work :) – Joe Enos Nov 25 '14 at 17:12
  • @KamranAhmed - As the question is closed as duplicate you may please edit your answer to use (new Date(item.date)).toISOString() so that the question is accurately answered. It might help the user who asked this completely. – Alok Swain Nov 25 '14 at 17:23
1

Use map(). Example:

var test = [
    {
        "date": "2014-04-23 00:00:00",
        "volumetrie": "22458"
    },
    {
        "date": "2014-05-02 00:00:00",
        "volumetrie": "30585"
    },
    {
        "date": "2014-03-27 00:00:00",
        "volumetrie": "49536"
    }
];
var newArr = test.map(function(key){
    return {x: key.date, y: key.volumetrie};
});
console.log(newArr);
MH2K9
  • 11,951
  • 7
  • 32
  • 49