0
flights.forEach(function(flight) {
    var origin = flight.origin,
    destination = flight.destination,
    count_airport = +flight.count,

    var z = "";

    for (i in ListByAirline) {        
      z = i + '_count';
      ListByAirline[i] = flight[z];
     };

    data_series_1[origin] = ListByAirline;
});

I am having problems with the for loop. For some reason all my elements in data_series_1 are just being mapped to the last iteration. Can anyone help?

To clarify - here are the inputs/outputs:

data_series_1 = {},

  ListByAirline = {
    'AA' : [],
    'AS' : [],
    'B6' : [],
    'DL' : [],
    'EV' : [],
    'F9' : [],
    'MQ' : [],
    'OO' : [],
    'UA' : [],
    'US' : [],
    'VX' : [],
    'WN' : []
  };

Flight data looks like this:

0: Object AA_cancelled: "" AA_count: "" and so on...

The output for the data_series_1 is:

ALB: Object AA: "" AS: "" B6: "" DL: "" EV: "" F9: "3" MQ: "" OO: ""
UA: "" US: "" VX: "" WN: ""

For EVERY origin (key)...so it is not iterating throughout and assigning the correct values to all the keys

Bilal
  • 61
  • 7
  • 1
    `ListByAirline` is undefined above – Vicky Gonsalves Sep 22 '15 at 02:40
  • See http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript – user2864740 Sep 22 '15 at 02:41
  • In any case the 'problem' is unclear, although I can think of at least two different explanations depending on the reading of 'all .. are just .. mapped to last iteration'. Create a minimal SSCCE - including minimal input and minimal (bad) output. – user2864740 Sep 22 '15 at 02:43
  • To consider: an assignment of an array *does not create a copy/clone/duplicate*. – user2864740 Sep 22 '15 at 02:46
  • It is defined (not included in this piece). – Bilal Sep 22 '15 at 02:52
  • Alright, then how is `flights` defined? – Spencer Wieczorek Sep 22 '15 at 03:04
  • flights is a gigantic array with basically a key (original location aka origin) which is mapped to a bunch of fields such as count, cancelled_flights etc – Bilal Sep 22 '15 at 03:06
  • So I am essentially trying to map the 'AA' key in my list to 'AA_count' for one origin/location then do the same for all the other carriers ('AS' to AS_count etc.) Then repeat for all locations' – Bilal Sep 22 '15 at 03:07
  • if you can provide a small chunk of real data with real data structures people can help you easier, to be honest I can do it, but I will not build your data structures to try the solution, i just wanna copy and paste your structures and build the solution, and test it, you should make it easy for us. – ncubica Sep 22 '15 at 03:46

1 Answers1

0

Yo have a single ListByAirline object, which you are updating in each iteration, but every data_series_1[origin] is referencing the same ListByAirline object

Something like this may be what you want to do

flights.forEach(function(flight) {
    var origin = flight.origin,
    destination = flight.destination,
    count_airport = +flight.count

    var z = "";

    var dest = data_series_1[origin] = {};
    for (i in ListByAirline) {        
        z = i + '_count';
        dest[i] = flight[z];
    };
});

In this scenarion, the ListByAirline object is simply a lookup for the keys used in data_series_1, so the for ... in loop could be done differently, perhaps like this

ListByAirline = ['AA','DL']; // a list of keys in an array

flights.forEach(function(flight) {
    var origin = flight.origin,
    destination = flight.destination,
    count_airport = +flight.count

    var z = "";

    var dest = data_series_1[origin] = {};
    ListByAirline.forEach(airline) { // changed this to a forEach
        z = airline + '_count';
        dest[airline] = flight[z];
    };
});
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87