0

I am working on Google charts API and Google Visualization Candlestick Charts expects data to be an array of arrays for it to work .

From back end i am reciving data in this format

[  
   {  
      "current_day":"2011-08-01",
      "open_val":136.65,
      "high_val":136.96,
      "low_val":134.15,
      "close_val":134.15
   },
   {  
      "current_day":"2011-08-02",
      "open_val":135.26,
      "high_val":135.95,
      "low_val":131.50,
      "close_val":131.85
   }
]

Could you please tell me how can i construct this data to the below format

[
[ "2011-08-01", 136.65, 136.96, 134.15, 136.49 ],
[ "2011-08-02", 135.26, 135.95, 131.50, 131.85 ]
]

to feed it to Candlestick input format data in google charts .

my fiddle

http://jsfiddle.net/Lm5mLg1d/

Pawan
  • 31,545
  • 102
  • 256
  • 434

3 Answers3

1

You'll need a list of the properties, in order, since JavaScript object properties are unordered by definition.

With that, it's straightforward:

var props = [
  "current_day", "open_val", "high_val", "low_val", "close_val"
];

var data = [  
   {  
      "current_day":"2011-08-01",
      "open_val":136.65,
      "high_val":136.96,
      "low_val":134.15,
      "close_val":134.15
   },
   {  
      "current_day":"2011-08-02",
      "open_val":135.26,
      "high_val":135.95,
      "low_val":131.50,
      "close_val":131.85
   }
]

var arrays = data.map(
  function (o) {
    var array = [];
    
    props.forEach(
      function (prop)
      {
        array.push( o[prop] );
      }
    );
    
    return array;
  }
);

console.log(arrays);

Or, for older browsers lacking map() and forEach():

var props = [
  "current_day", "open_val", "high_val", "low_val", "close_val"
];

var data = [  
   {  
      "current_day":"2011-08-01",
      "open_val":136.65,
      "high_val":136.96,
      "low_val":134.15,
      "close_val":134.15
   },
   {  
      "current_day":"2011-08-02",
      "open_val":135.26,
      "high_val":135.95,
      "low_val":131.50,
      "close_val":131.85
   }
]

var arrays = [];

for ( var i = 0; i < data.length; ++i )
  {
    var array = [];
    
    for ( var j = 0; j < props.length; ++j )
      {
        array.push( data[i][props[j]] );
      }
    
    arrays.push(array);
  }

console.log(arrays);
Community
  • 1
  • 1
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • thanks a lot its working fine , but why are you returning the array , do i need to call the function and supply the jsonobect to it ?? – Pawan Nov 05 '15 at 14:41
  • What if the objects acquire or lose a property, or this property changes it's name ?, or even if some object doesn't have one of them, or has a different one? – Luis Sieira Nov 05 '15 at 14:42
  • @PreethiJain It's just the return from `map()`'s inner function. – Paul Roub Nov 05 '15 at 14:42
  • @LuisSieira Then `props` needs to be updated; it's still the only way to enforce the requested order. Without an explicit list, a JS implementation *could*, legally, return a different order for *every single object*. – Paul Roub Nov 05 '15 at 14:43
  • @PaulRoub I didn't think about that. Thanks – Luis Sieira Nov 05 '15 at 14:48
0

You can try this

You need to map the array, so each new term is the value of each key of the contained object, so:

var stuff = [
   {  
      "current_day":"2011-08-01",
      "open_val":136.65,
      "high_val":136.96,
      "low_val":134.15,
      "close_val":134.15
   },
   {  
      "current_day":"2011-08-02",
      "open_val":135.26,
      "high_val":135.95,
      "low_val":131.50,
      "close_val":131.85
  }
];

var result = stuff.map(function (object) { 
    var arr = [];
  for(var prop in object) {
    arr.push(object[prop]);
  }
  return arr;
});

alert(result);
Luis Sieira
  • 29,926
  • 3
  • 31
  • 53
  • Thank you but when i ran your code i am getting Block-scoped declarations (let, const, function, class) not yet supported outside strict mode in browser console . – Pawan Nov 05 '15 at 14:35
  • I'll link you a plunkr, it's because I used let. Change let to var, and it should work, I'm making a plunkr – Luis Sieira Nov 05 '15 at 14:37
  • [JavaScript properties are unordered](http://stackoverflow.com/a/5525820/1324). You can't rely on `for...in` returning `current_day` first, etc. – Paul Roub Nov 05 '15 at 14:37
  • Thank you , now i am getting Uncaught TypeError: Array.map is not a function – Pawan Nov 05 '15 at 14:38
0

You can simply write a function to achieve this. Something like below should work:

var myJson = [{
    "current_day": "2011-08-01",
        "open_val": 136.65,
        "high_val": 136.96,
        "low_val": 134.15,
        "close_val": 134.15
}, {
    "current_day": "2011-08-02",
        "open_val": 135.26,
        "high_val": 135.95,
        "low_val": 131.50,
        "close_val": 131.85
}];
var myArr = [];
var i = 0;
for (i = 0; i < myJson.length; i++) {
    myArr.push({
        myJson.current_day, myJson.open_val, myJson.high_val, myJson.low_val, myJson.close_val
    });
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Sachin Siwach
  • 90
  • 1
  • 10