1

I have a Javascript object called Data that looks like:

Object {
  zip: Array[14631],
  lat: Array[14631],
  lng: Array[14631],
  surge: Array[14631]
}

Data looks like

Zipcode, Lat,    Lng,    Surge
10025,   47.323, -93.43, 1.7

So to access elements I can do something like: data['zip'][i] and it will return the 'ith' element of the zip array. I can do the same with all the arrays.

What I want is a data structure such that I can do: data[10025] and it will return all matching element in the other arrays. So lets say I had 5 entries with zipcode = 10025, the data[10025] would return:

10025,lat_1,lng_1,surge_1
10025,lat_2,lng_2,surge_2
10025,lat_3,lng_3,surge_3
10025,lat_4,lng_4,surge_4
10025,lat_5,lng_5,surge_5

So that I could then double iterate through

for zip in data['zip']{
  for i in data[zip] {
    print data[zip]['lats'][i]
  }
}

I think this is a simple enough problem but I don't have much experience with javascript and can't seem to get it. Any help is much appreciated.

Another way to phrase this is I would like an object that looks like:

    data[array size of N]

Where each element of that array holds 3 arrays of size M

2
  • please add some parts of the real data structure. Commented May 27, 2016 at 18:23
  • What parts do you think I should add? The real structure is an object with 4 arrays of equal length. Commented May 27, 2016 at 18:25

3 Answers 3

2

Why not do it something like this...

data = {
    "10025": [
        {
            "Lat": 47.323,
            "Lng": -93.43,
            "Surge": 1.7
        },{
            "Lat": 47.323,
            "Lng": -93.43,
            "Surge": 1.7
        },{
            "Lat": 47.323,
            "Lng": -93.43,
            "Surge": 1.7
        }
    ]
};

Then you could access the zip by data["10025"] and loop through the array of locations.

var data = {};
for(var i = 0; i < obj.zip.length;i++){
    var zip = obj.zip[i]
    data[zip] = data[zip] || [];
    data[zip].push({
        zip: obj.zip[i],
        lat: obj.lat[i],
        lng: obj.lng[i],
        surge: obj.surge[i]
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ideally that would be the best way to do it. But the data is given to me in the format I described. I want to turn it into the format you describe above.
James, I added the algorithm to achieve your proposed data structure. Feel free to roll back the edit if you do not agree.
Thanks guys, that did the trick. Is there an easy way I can iterate through the zipcodes now that they a keys?
2

Without changing the original data, you could use the indices of the array in a new array for an object with the zip code as key.

data = {
    "10025": [ 2, 4, 7, 10],
    "10027": [ 1, 3, 9, 11],
};

Generate data with

var data = Object.create(null); // create empty object

object.zip.forEach(function (z, i) {
    data[z] = data[z] || [];
    data[z].push(i);
});

Access would be:

data[zip].forEach(function (index) {
    // do something with the value ...
    var lat = object.lats[index]
});

1 Comment

How would I do that in Javascript given just the object Data, I described?
0
// Array, keyed by zip code
var zip_array = [];

// Iterate over zip data, building zip_array as we go.
var zip_data = data.zip;
for (var i = 0, len = zip_data.length; i < len; ++i)
{
    var current_zip = zip_data[i];
    if (zip_array[current_zip]){
      zip_array[current_zip].push(getDataAt(i));
    }
  else {
    zip_array[current_zip] = [ getDataAt(i) ];
  }
}

function getDataAt (index)
{
  return [ data.zip[index], data.lat[index], data.lng[index], data.surge[index]].join(",");
}

1 Comment

POST QUESTION-UPDATE: If you want zip_array to be a 3d array (as you describe), instead of 2d as I have implemented here, then just omit the .join(",") in the final line of getDataAt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.