JS: mapping an array inside a push

17,909

Solution 1

If your current code is working, then you're quite right that $.map can do the same job:

chart.push({
    area: true,
    color: '#FFBA78',
    values: $.map(data[1], function(entry) {
        return {x: entry.date, y: Math.round(entry.value)};
    })
});

Updated Fiddle

Or using Array#map from ES5 (which can be shimmed on older browsers):

chart.push({
    area: true,
    color: '#FFBA78',
    values: data[1].map(function(entry) {
        return {x: entry.date, y: Math.round(entry.value)};
    })
});

Updated Fiddle

Solution 2

Using ES5 Array.prototype.map:

data['1'].map(function(item){ return  {x: item.date, y: Math.round(item.value)}  } )

Where item represent the date, value object in your array.

Important: this will work on IE9+ if you need this to run in older version of IE please follow the polyfill instructions here.

You can also check some of the performance benchmakrs between the 2 here:

http://jsperf.com/jquery-map-vs-array-map

The browser support and the performance are probably the key areas that you want to review in order to go for this option.

Share:
17,909

Related videos on Youtube

ditto
Author by

ditto

Updated on June 04, 2022

Comments

  • ditto
    ditto almost 2 years

    Is there a better way to get the bio object into the pushed chart values?

    // date and value keys.
    var data = {"1":[{"date":"2014-03-10","value":14},{"date":"2014-03-17","value":15},{"date":"2014-03-19","value":13},{"date":"2014-04-11","value":11},{"date":"2014-04-13","value":13.7},{"date":"2014-04-14","value":14.6},{"date":"2014-04-15","value":17},{"date":"2014-04-17","value":9},{"date":"2014-04-20","value":10},{"date":"2014-04-21","value":17},{"date":"2014-04-24","value":15},{"date":"2014-05-02","value":10},{"date":"2014-05-03","value":95.3},{"date":"2014-05-09","value":92.1},{"date":"2014-05-12","value":3},{"date":"2014-05-14","value":88.9},{"date":"2014-05-15","value":95.3},{"date":"2014-05-23","value":82.6},{"date":"2014-05-24","value":95.3},{"date":"2014-05-30","value":99},{"date":"2014-05-31","value":88.9},{"date":"2014-06-01","value":80},{"date":"2014-06-17","value":82},{"date":"2014-07-08","value":95},{"date":"2014-07-30","value":127},{"date":"2014-08-02","value":90},{"date":"2014-08-03","value":80},{"date":"2014-08-09","value":82}],"2":[{"date":"2014-03-10","value":"1"},{"date":"2014-03-19","value":"23"},{"date":"2014-04-11","value":"14"},{"date":"2014-04-13","value":"14.4"},{"date":"2014-04-14","value":"14"},{"date":"2014-04-21","value":"11"},{"date":"2014-04-24","value":"13.5"},{"date":"2014-05-04","value":"4"},{"date":"2014-05-15","value":"15"},{"date":"2014-05-17","value":"16"},{"date":"2014-05-23","value":"9.3"},{"date":"2014-05-24","value":"11"},{"date":"2014-05-25","value":"14.9"},{"date":"2014-06-01","value":"14.1"}]};
    
    var bio = [];
    $.each(data['1'], function(date, value) {
        bio.push({x: value['date'], y: Math.round(value['value'])});
    });
    
    chart = [];
    chart.push({
        area: true,
        color: '#FFBA78',
        values: bio,
    });
    

    Basically what I'm doing is taking the data['1'] array, and for each node I'm changing the keys: date into x, and value into y. I think this may be possible with an array map? Something like:

    chart.push({
        area: true,
        color: '#FFBA78',
        values: data['1'].map(function() { ... }),
    });
    

    Here's a JSFiddle.

    • T.J. Crowder
      T.J. Crowder over 9 years
      "...and for each node I'm changing the keys: date into y, and values into x..." No, you're changing date into x, and if you have a values key in data, I'm not seeing in it the quoted data.